Lesson 17 – Working with Acceleration Sensor ADXL345
Booting up the Raspberry Pi
- This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
- If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
- Let’s now get started and boot up the Raspberry Pi.
- Grab a good quality USB cable and a USB power adaptor (2.5A).
- Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
- This should now power up the Raspberry Pi.
- Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
- If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
- Else get connected to the Raspberry Pi using VNC.
Let’s Get Started
- Our next tutorial is called, “Working with Acceleration Sensor ADXL345”.
- We will start off this tutorial by first putting together the circuit using a breadboard.
- Components required for this circuit include –
- 1 * Raspberry Pi
- 1 * Breadboard
- 1 * T-Extension Board
- 1 * ADXL345 module
- 1 * 40-Pin GPIO Cable
- Jumper wires
- The below diagram provides a view of what the circuit should look like.
- Click on the following <Link> to open up the tutorial.
- Step through the instructions provided and put together the circuit.
- Once you have put together the circuit, open up the python editor and start writing your code.
PLEASE NOTE – Please make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.
Let’s Write Some Python Code
Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……
#!/usr/bin/env python from I2C import I2C from time import sleep class ADXL345(I2C): ADXL345_ADDRESS = 0x53 ADXL345_REG_DEVID = 0x00 # Device ID ADXL345_REG_DATAX0 = 0x32 # X-axis data 0 (6 bytes for X/Y/Z) ADXL345_REG_POWER_CTL = 0x2D # Power-saving features control ADXL345_DATARATE_0_10_HZ = 0x00 ADXL345_DATARATE_0_20_HZ = 0x01 ADXL345_DATARATE_0_39_HZ = 0x02 ADXL345_DATARATE_0_78_HZ = 0x03 ADXL345_DATARATE_1_56_HZ = 0x04 ADXL345_DATARATE_3_13_HZ = 0x05 ADXL345_DATARATE_6_25HZ = 0x06 ADXL345_DATARATE_12_5_HZ = 0x07 ADXL345_DATARATE_25_HZ = 0x08 ADXL345_DATARATE_50_HZ = 0x09 ADXL345_DATARATE_100_HZ = 0x0A # (default) ADXL345_DATARATE_200_HZ = 0x0B ADXL345_DATARATE_400_HZ = 0x0C ADXL345_DATARATE_800_HZ = 0x0D ADXL345_DATARATE_1600_HZ = 0x0E ADXL345_DATARATE_3200_HZ = 0x0F ADXL345_RANGE_2_G = 0x00 # +/- 2g (default) ADXL345_RANGE_4_G = 0x01 # +/- 4g ADXL345_RANGE_8_G = 0x02 # +/- 8g ADXL345_RANGE_16_G = 0x03 # +/- 16g def __init__(self, busnum=-1, debug=False): self.accel = I2C(self.ADXL345_ADDRESS, busnum, debug) if self.accel.readU8(self.ADXL345_REG_DEVID) == 0xE5: # Enable the accelerometer self.accel.write8(self.ADXL345_REG_POWER_CTL, 0x08) def setRange(self, range): # Read the data format register to preserve bits. Update the data # rate, make sure that the FULL-RES bit is enabled for range scaling format = ((self.accel.readU8(self.ADXL345_REG_DATA_FORMAT) & ~0x0F) | range | 0x08) # Write the register back to the IC seld.accel.write8(self.ADXL345_REG_DATA_FORMAT, format) def getRange(self): return self.accel.readU8(self.ADXL345_REG_DATA_FORMAT) & 0x03 def setDataRate(self, dataRate): # Note: The LOW_POWER bits are currently ignored, # we always keep the device in 'normal' mode self.accel.write8(self.ADXL345_REG_BW_RATE, dataRate & 0x0F) def getDataRate(self): return self.accel.readU8(self.ADXL345_REG_BW_RATE) & 0x0F # Read the accelerometer def read(self): raw = self.accel.readList(self.ADXL345_REG_DATAX0, 6) print raw res = [] for i in range(0, 6, 2): g = raw[i] | (raw[i+1] << 8) if g > 32767: g -= 65535 res.append(g) return res def print_msg(): print ("========================================") print ("| ADXL345 |") print ("| ------------------------------ |") print ("| SCL connect to SCL |") print ("| SDA connect to SDA |") print ("| |") print ("| Read value from ADXL345 |") print ("| |") print ("| SunFounder|") print ("========================================\n") print 'Program is running...' print 'Please press Ctrl+C to end the program...' raw_input ("Press anykey to begin\n") # Simple example prints accelerometer data once per second: def main(): accel = ADXL345() while True: x, y, z = accel.read() print 'X: %d, Y: %d, Z: %d'%(x, y, z) print '' sleep(1) # Output is fun to watch if this is commented out def destroy(): exit() if __name__ == '__main__': try: main() except KeyboardInterrupt: destroy()
About the Raspberry Pi
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.
The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.
You can read more about the Raspberry Pi here – RaspberryPi.org.
Prerequisites
- This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
- You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi kit to be able to work on these tutorials.
- If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
- Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
- You can read more about the Raspberry Pi here – RaspberryPi.org.