Do you have smart phone or an iPad that switches the screen orientation when you rotate it? Ever wonder how that works? It's done using a component called an accelerometer, and there are lots of cool applications for them when designing and building electronics.
The RaspberrySTEM kit includes it's own accelerometer -- it's in the bag marked Accelerometer RaspberrySTEM Cell. Feel free to pull it out now...
By plugging in the accelerometer to the RaspberrySTEM and then writing code to get input from it, we can turn our RaspberrySTEM into a controller that can detect magnitude of movement (acceleration) along all three axes -- left/right, front/back and up/down. From this information, we can determine the orientation of the kit and/or the vector (speed and direction) of movement at a given time. I know that might sound a little confusing, but follow along and you'll see what we mean.
Your Accelerometer RaspberrySTEM Cell contains a single part -- the accelerometer circuit board -- that looks like this (from the top):
Let's wire up your accelerometer and write some code to get it working...
Step #1: Place the
accelerometer on the breadboard
The accelerometer has six pins coming from the bottom of it. It should be inserted into the breadboard such that each pin sits in a different connect strip, and you should ensure that at least one other hole is usable in each connect strip where the accelerometer sits.
It is very important to note that the power and the ground of the accelerometer must be connected appropriately -- 3.3V power from the lid connector must be connected to VCC on the accelerometer and GND from the lid connector must be attached to GND on the accelerometer. If the accelerometer is turned around 180 degrees, you'll notice that VCC will be located where GND should be and vice-versa.
If you hook up GND on the accelerometer to 3.3V on the lid connector and you hook up VCC on the accelerometer to GND on the lid connector, there is a good chance that you will destroy the accelerometer chip (it will likely get warm and start to smoke). So, be very careful that you hook up the chip as indicated above and below.
Step #2: Attach power and ground
To wire the accelerometer, the first thing you'll need to do is to run a wire from VCC on the accelerometer to 3.3V power somewhere on the breadboard. Next, run a wire from GND on the accelerometer to GND somewhere on the breadboard.
Step #3: Attach SDA pin to the connector board
Next, you'll need to attach a wire from the connect strip attached to the SDA pin on the accelerometer to the SDA pin on the connector board.
Step #4: Attach SCL pin to the connector board
Finally, you'll attach a wire from the connect strip attached to the SCL pin on the accelerometer to the SCL pin on the connector board.
Here is what your breadboard should look like once the accelerometer is properly wired:
Let's write some code to test your accelerometer and see how it works:
Now, let's take a look at what our code is doing:
On Line 1, we import the module we will need to initialize and get information from the accelerometer (called Accel())
On Line 2, we import the time module, as we're going to be using a loop and will want to slow the repetition of the loop down a bit
On Line 4, we initialize our accelerometer. We call the Accel() function, which returns an "accelerometer object" to our accel variable. We talked about that a bit in VARIABLES & ASSIGNMENTS, but the important thing to understand is that we can now use accel to call other accelerometer functions.
On Line 6, we start a loop (this is where we'll spend all of our time once we run the program)
On Line 7, we call the accelerometer function forces(), which returns three values, representing the forces in the x-direction, y-direction and z-direction
On Line 8, we print those three values to the screen
On Line 9, we pause briefly (so we don't overwork the processor) and then return to the top of the loop where we read the new values from the accelerometer
If you run your test code, what you should immediately see is that your Output Window starts filling up with numbers. These numbers -- three per line -- indicate the relative movement of the RaspberrySTEM (specifically, the accelerometer we wired on the breadboard), as follows:
The first number indicates the left/right tilt of the RaspberrySTEM -- if you tilt to the left, the number increases towards 1 and if you tilt to the right, the number decreases towards -1.
The second number indicates the front/back tilt of the accelerometer -- if you tilt backwards, the number increases towards 1 and if you tilt forwards, the number decreases towards -1.
The third number indicates the up/down movement of the accelerometer -- if you move it upwards, the number increases towards 1 and if you move it downwards, the number decreases towards -1.
Play around with the RaspberrySTEM and try to get a feel for how different movements and rotations affect the numbers on the screen. In future projects, we'll be using this functionality to turn the RaspberrySTEM into a game controller.