Do you have a 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 CREATOR Kit includes its 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 CREATOR Kit and then writing code to get input from it, we can turn our RaspberrySTEM CREATOR Kit into a controller that can detect changes in 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.
Your Accelerometer RaspberrySTEM Cell contains a single part -- the accelerometer circuit board -- that looks like this (from the top):
On the circuit board above, you'll see a diagram with 3 arrows, labeled X, Y and Z. Note that it looks like only two arrows, X and Y. The third arrow, the Z arrow, is somewhat hidden -- it is a circle with a dot. The Z arrow is actually pointing up perpendicularly from the circuit board, and it shows the front tip of the arrow.
The accelerometer measures forces -- this can be the forces it feels when you move it around, or simply the force of gravity. When the RaspberrySTEM CREATOR Kit is sitting on a table and not moving, it reports just the force of gravity, in the X, Y, and Z directions:
When an arrow points up, the force will be 1.
When an arrow points down, the force will be -1.
When an arrow points neither up nor down (perpendicular to the force of gravity), the force will be 0.
When an arrow is tilted somewhere in between directly down and directly up, then the force will be somewhere in between -1 and 1.
For example, when the RaspberrySTEM CREATOR Kit is sitting flat on a table, the Z arrow points up, and the X and Y arrow are parallel with the table. In that case, the accelerometer will report an X, Y and Z of approximately 0, 0, and 1. If you flip the RaspberrySTEM CREATOR Kit over, so the Z arrow points down towards the earth, the accelerometer will report an X, Y and Z of approximately 0, 0, and -1.
Let's wire up your accelerometer and write some code to get it working.
The accelerometer has six pins. 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. When inserting the accelerometer into the breadboard, don't be afraid to apply some pressure from the top -- the bottom of the black plastic connectors should touch the breadboard.
It is very important to note that the power and the ground of the accelerometer must be connected appropriately, otherwise 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 shown below.
With four wires, wire up the accelerometer as show below. Specifically:
Wire GND on the accelerometer to GND on the breadboard.
Wire VCC on the accelerometer to 3.3V power on the breadboard.
Wire SCL on the accelerometer to SCL on the Lid Connector Board.
Wire SDA on the accelerometer to SDA on the Lid Connector Board.
Here is what your breadboard should look like once the accelerometer is properly wired (the LED Matrix in this picture can be ignored):
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 three numbers on each line are the X, Y and Z forces.
If you sit your RaspberrySTEM CREATOR Kit down on a flat surface, you should expect the first number to be 0, the second number to be 0 and the third number to be 1. In reality, the accelerometer is very sensitive, and it's unlikely that your surface is 100% flat, so you'll probably see readings that are very slightly off from those numbers.
Play around with the RaspberrySTEM CREATOR Kit 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 CREATOR Kit into a game controller.