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.
This is a post about how to get started working with the ESP8266 module and start doing interesting things with it. I will describe what we created, which is specific to our project, but a lot of the things are common to a lot of different applications for the module.
The first thing you’ll find when you google the ESP8266 module is that there are a lot of different form factors for the module. It comes in a variety of shapes and sizes, each of which finds a specific balance between ease-of-use and size. I’ll just talk about the ones we were dealing with. There are probably a few more varieties of this around.
This is adafruit’s version. This is probably the easiest one to work with. It’s got clean pins that expose all of it’s functionality and can easily be put on a breadboard for experimentation.
This is the much smaller version. It is remarkably tiny. It’s hard to believe that this little guy has a Wifi module as well as a pretty respectable microcontroller inside. And all for just about $3! One problem with this is that, as you might have noticed, it has only 8 pins. Vcc, Gnd, Tx, Rx, Chip PowerDown and Reset make up 6 of them, leaving just 2 GPIO pins. So not as much flexibility as the previous module. But still defenitely a good choice for limited functionality.
Finally, this is another version. It has all the pins like the Huzzah, just in a non-breadboard-friendly format. This is the sort of module you would use when you have fabricated a PCB for your specific needs and need to mount your Wifi chip onto it (although that didn’t stop us from experimenting with this on a breadboard anyway). It’s about the same size as the previous form factor, with all the functionality of the first one.
Some of these modules (NodeMCU) come with a Lua toolchain. You use Lua on your development environement. However, there is an Arduino toolchain too (which is probably the best way to go if you are coming from the arduino environment and have used their IDE before). There’s good documentation on how to get this set up and running in the Arduino environment so I won’t go into that. It’s very easy and once done, gives you the very familiar Arduino IDE to use as you would any Arduino board.
Once your PC/Mac is set up, we need to connect the module to it. The Huzzah has a very convenient micro USB port that you can use to plug it directly into the computer. If you are using the other types, you will need to use a USB to TTL serial converter, that looks something like this:
This is an RFduino USB Shield that we happened to have with us. What you are looking for is something that converts from USB to serial (TX and RX).
Once that’s done, you are ready to plug the module in. The connections required for each module type are slightly different. For the 8-pin module, this is how everything is connected up:
Notice the dashed black line! This is connected to GPIO0. This is important because you must connect this only when uploading new code to the module (flashing it). Whenever this pin is connected to ground, it assumes new code is coming in through the TX and RX pins, and goes into flash mode. Once you are done programming, remember to remove that connection! Otherwise the board will get reprogrammed with junk values.
Just to clarify, this is what this diagram will look like once you are done programming and are just running the program.
On this module, you will need to do a few more connections. The above connections are largely the same, with a few extra steps:
Here’s our connected board. There are a lot more connections here because of all the peripherals, but we will get to that soon. For now, just take comfort in the fact that your board is probably not as messy as ours.