Testing our accelerometer

We are now ready to test our ADXL345 to check that we can read the data from it into Python. Power up your Pi Zero and connect to it over SSH. Move into your project directory by typing cd WearableTech, and then make a new directory for this chapter by typing mkdir Chapter4 ; and now move into that new directory by typing cd Chapter4.

Pimoroni has provided a Python library for the Adafruit ADXL345 accelerometer, which is available on their GitHub page at https://github.com/pimoroni/adxl345-python ; however, this library is written for Python 2, and we are using Python 3. I have cloned this repository in GitHub and adjusted it so that it works for Python 3. We will make use of this library to access and test our ADXL345.

Before we access the ADXL345 over Python, we should check that it is being correctly detected on the I2C bus. If you didn't follow the project in Chapter 2, Scrolling LED Badge, or you are carrying this project out on a new SD card, and then you will need to enable the I2C bus on your Pi Zero. Head over to Chapter 2, Scrolling LED Badge, and follow the steps there if you need to.

Now, we have the I2C bus enabled, issue the following command to test, which address our ADXL345 I2C device is detected on, sudo i2cdetect -y 1. You should see something like this:

This is telling us that we have a device on address 53 of the I2C bus, which is good because this is the address that we are expecting ADXL345 to be present on!

Now, we are going to clone the modified GitHub repository and check whether we can get readings from our ADXL345 into Python. We need to install Git to carry these steps out, but before installing the new software, it is best to update and upgrade what we have already. Issue this command to update and upgrade the software on your Pi Zero:

sudo apt-get update && sudo apt-get upgrade -y

When this completes, we can now install Git. Git is a software version control system, and GitHub is a web-based Git system, which many developers around the world use for sharing and collaborating on software projects together. Issue the following command to install Git:

sudo apt-get install git -y

Once the install process completes, we can now copy or clone the GitHub repository from my GitHub account with the Python library we need to communicate with ADXL345 in Python 3. Type the following command to clone the ADXL345 libraries into the current directory:

git clone https://github.com/jonwitts/adxl345-python.git

If you type ls -l, you will notice that you now have a new directory called adxl345-python; move into this directory by typing cd adxl345-python, and then list the contents of this directory by typing ls -l. You will see something similar to this picture:

The file we are interested in here is example.py. Run it by typing this:

python3 ./example.py

All being well, you should see something like this picture being output to your SSH connection:

If you rotate your Pi Zero through its three rotational axis, you should see these values changing. We are reading the force on ADXL345 in a measurement of g, the gravitational pull from the earth's gravity. As we rotate and move ADXL345 through each of its axis, we can see that reading of force change. Stop the example.py program from running by pressing Ctrl + C on your keyboard.

Once we have stopped the ADXL345 example from running, we can turn off our Pi Zero by holding your power-off switch in for three seconds. Leave it for a few seconds after the light finishes flashing to allow the Pi Zero to shut down completely, and then remove the power from your Pi Zero.

Next, we are going to look at connecting Blinkt to our Pi Zero.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.13.201