For our project, we needed a bunch of peripherals to be connected to the primary wifi module. I will go over each of them individually. Here’s the list:
This post will be about the accelerometer. Look out for the other peripherals in upcoming posts.
There are a few things you want to look out for while choosing the accelerometer for your project. You can get modules with one, two and three accelerometers, for 1D, 2D and 3D sensing. Sometimes, what you are really looking for might be Inertial Measurement Units (IMUs). These have gyroscopes, which, together with the accelerometers, provide very accurate direction and orientation information too. In our case though, we went with a 3-axis
accelerometer (as it doesn’t really matter which direction beachballs are facing).
We came across two main types of accelerometers - Analog and digital (using the I2C protocol). We chose the digital one because it’s super easy to implement (you just need the appropriate library file in your software environment) and requires just two pins. The analog one will need 3 Analog-to-Digital converter pins to be able to get the same information.
Connecting the accelerometer to the ESP8266 is very straightforward. There are 4 pins you need to connect in total - VCC, GND, SDA and SCL. We used an MMA8451.
This is how we connected them all up:
So just connect SCL and SDA to pins 4 and 5 respectively. The VIN and Ground pins are connected to some common power and ground wires that run through the system.
Alright, so let’s get into some code now. Let’s see how to get the accelerometer to talk to the ESP.
The accelerometer requires some calibration initially. Hence the random numbers in the setup() function. Note that these numbers are based on keeping the module on a table and measuring the values. They change slightly as the module is rotated! This is where the gyroscope would come into play, letting the accelerometer know exactly how the module is being rotated so it can make corrections. Anyway, we don’t need very accurate readings, just need to detect spikes in the acceleration caused by impacts, so we should probably ok even with all the error in measurement.
Notice how in the Wire.begin() command we give 4 and 5 as parameters. Those are the pins we connected the SCL and SDA pins of the accelerometer to.