1 minute read
How to Use an Accelerometer
206 Hacking Electronics
The main loop reads the value from the analog input and then does a bit of arithmetic to calculate the actual temperature.
First, the voltage at the analog input is calculated. This will be the raw value (between 0 and 1023) divided by 205. It is divided by 205 because a span of 1024 values occupies 5V, or 1024 / 5 = 205 per volt.
The TMP36 outputs a voltage from which the temperature in degrees C can be calculated from the equation:
tempC = 100.0 * volts – 50
For good measure, the sketch also converts this into degrees F and prints both out to the Serial Monitor.
void loop() { int raw = analogRead(sensePin); float volts = raw / 205.0; float tempC = 100.0 * volts - 50; float tempF = tempC * 9.0 / 5.0 + 32.0; Serial.print(tempC); Serial.print(" C "); Serial.print(tempF); Serial.println(" F"); delay(1000); }
How to Use an Accelerometer
Tiny accelerometer modules (Figure 8-14) are now available at low cost. The two models shown are very similar, both being 5V compatible and providing analog outputs for each axis. The one on the left is from Freetronics (www.freetronics.com/am3x) and the one on the right is from Adafruit (www.adafruit.com/products/163).
These modules are three axis accelerometers that measure the force applied to a tiny weight inside the chip. Two of the dimensions, X and Y are parallel to the modules PCB. The third dimension (Z) is at 90 degrees to the module’s surface. There will normally be a constant force acting on this dimension due to gravity. So if you tip the module, the effect of gravity starts to increase on the dimension in which you tip it (see Figure 8-15).
Figure 8-14 Accelerometer modules