2018-03-26 23:24:18 by Tim
I have several data logging projects I am going to be working on and wanted to create a standard general purpose data logger that I could plug different sensors into (for example temperature) and be able to save the data to a file as well as have a simple display. I also wanted the capability to connect to an IoT service using WiFi for future features.
The Tech Stack
I was going to start with the Arduino as the brains for the logger but thought this would be a good time to try out the ESP32 by Espressif (http://espressif.com/en/products/hardware/esp32/overview). The ESP32 is the 32bit successor to the very popular ESP8266 which had integrated WiFi and really took off in popularity as it made adding connectivity to the Arduino platforms so much easier.
The ESP32 can use the Arduino IDE for programming and has built-in WiFi and BLE along with lots of GPIO pins. In terms of development boards probably the two best available are the Sparkfun ESP32 Thing (https://www.sparkfun.com/products/13907) and the Adafruit ESP32 Feather (https://www.adafruit.com/product/3405). I ended up choosing the Feather because Adafruit has built a whole new Feather line with add-on stacking modules and it seemed perfect for prototyping. Here’s what you get in the Feather ESP32 board (Huzzah32):
That’s a lot of features for 20 bucks.
So here’s what I ended up ordering:
- Feather ESP32 Development Board (https://www.adafruit.com/product/3405). Important to note there is that this board can be powered via the USB but also has built-in LiPo charger so can be run off of a battery which is a requirement for a data logger. Also all logic is 3.3V so if 5V is needed you will need a level shifter.
- Adalogger Featherwing (RTC+SD) (https://www.adafruit.com/product/2922). This featherwing module has a Real Time Clock and an SD card reader which will be used to time stamp and save data.
- Quad Alphanumeric Featherwing Display (https://www.adafruit.com/product/3130). Simple segment display to show data output to end user. Could also use an OLED display I think but it will depend on future battery testing with specific applications.
Getting Started with the ESP32 Feather Development Board
As usual Adafruit has a solid walk-through tutorial on setting up the ESP32 Feather: https://learn.adafruit.com/adafruit-huzzah32-esp32-feather
Some important notes taken from Adafruit tutorial on powering the ESP32 Feather:
Next you need to get the Arduino IDE working with the ESP32 Feather and there are several steps to follow:
- Download the SiLabs CP2104 Driver (USB chip) here: https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers . This allows the USB to act like a virtual COM port and to communicate through the Arduino IDE. Unzip the file and run the exe ‘CP210xVCPInstaller_x64′ or the x86’ version depending on your computer hardware.
After running the installer you should get Ready to Use message:
Next you can plug your ESP32 Feather into your USB port and then open Device Manager on Windows 10 and you should see the CP210x show up as a COM port:
- Follow the install instructions here: https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/windows.md to install ESP32 support in Arduino. You have to install Git as part of this but if you follow the instructions it should work.
Tip: you need to be in the esp32 folder to run the git command:
Now you can plug in your ESP32 board and start Arduino IDE, select COM3 (or your assigned COM port), select your board in Tools -> Board menu (select the Adafruit ESP32 Feather)
To run a simple program to verify that your board is working properly go to examples and open the chipid example:
Since this example uses a slower baud rate change the baud rate in the Tools -> Board menu to match (11.5kbps) and then upload the code. This code will simply poll the ESP32 and ask for it’s Chip ID and then echo that back in the serial monitor.
Now you have your ESP32 board up and running!
Setting Up the Real Time Clock with the Adalogger
If you want to have a data logger then you’re going to need two essential things: 1) a real time clock and 2) a way to store the data. The Adalogger featherwing gives you both!
Adafruit has a great write up on using the Real Time Clock in Arduino starting here: https://learn.adafruit.com/adafruit-adalogger-featherwing/using-the-real-time-clock.
Open the Library Manager in Arduino and install the Adafruit RTC library and then restart the Arduino IDE:
Now you can just follow along with the Adafruit tutorial and see how the RTC works with some built-in examples:
With the Adalogger featherwing stacked onto the ESP32 feather I can run the example RTC code and get the time stamp:
So now you have a Real Time Clock for your data logger!
Setting Up the SD Card for Data Storage with the Adalogger
Up next is the SD card which you need in a data logger for long term storage. You need to have a MicroSD card and an SD adapter to format and get the data onto your computer.
To format your SD card on Windows use the official SD Format app you can get it here: https://www.sdcard.org/downloads/formatter_4/. I have an 8GB card and this what it look like when I run the format tool:
Now when you insert the memory card into your computer Windows should automatically recognize it and it should show up as a working Removable Disk drive:
Next put the MicroSD card into the Adalogger and start the Arduino IDE.
Now you can follow along with the Adafruit tutorial on testing and writing to the SD card here: https://learn.adafruit.com/adafruit-adalogger-featherwing/using-the-sd-card?embeds=allow
For the ESP32 it’s important to note that the Chip Select line has to be set to the pin connection that the ESP32 uses which should be pin GPIO 33:
When I try and use the cardinfo example that uses the default Arduino SD library I get errors. This is because the ESP32 isn’t really an Arduino board so it’s going to be using the ESP32 SD library that was installed earlier.
Now Adafruit has an example called SD_Test that you can open up and run and it will read/write some files to your SD card for you. But when I first ran it I could get it to compile but it wasn’t doing anything else – not even printing to the serial monitor.
After doing some debugging and searching around I came across this article on using ESP32 generic board with SD library:
Here they are defining the SPI pins including the Chip Select. I also looked at the Readme file in the Adafruit ESP32 folder that was installed previously and found this line:
So then I tried modifying the code below to just specify the Chip Select pin (which is pin 33 from Adalogger pinout) :
And it worked!
Below are the files created on the SD card looking at them on a Windows PC.
So now you should be able to use this library and the Real Time Clock library to save sensor data with a time stamp to an SD card.
Using An Alpha Numeric Display
The last thing you will want for your data logger is display. You might not always need/want a display but we will use the Alpha Numeric featherwing by Adafruit to setup a display for our projects.
Here’s what it looks like stacked up with the ESP32 board and the Adalogger board:
Again Adafruit has a solid tutorial on how to setup and code this display in the Arduino IDE: https://learn.adafruit.com/14-segment-alpha-numeric-led-featherwing
Once you download the additional libraries you can load the example code and start sending your own messages:
Here’s what my display looks like:
By using the Adafruit ESP32 development board with add-on boards we were able to make a solid base for a universal data logging tool all in the Arduino environment. I’ll have future articles on creating specific types of data loggers, making a case, and more!