Tuesday, September 25, 2018

Italian Summer (2nd Part)

The air we breath and the water we drink are probably the most important aspects of our environment. While plenty of different chemicals could be around, particulate is considered as an indicator of industrial activity and PM2.5 is recorded as certain carcinogen.
So as promised I want to share my experience with the SDS011 PM Sensor and the esp8266 MCU (NodeMCU 1.0, ESP-12E), which I just got and connected together.

Step 1 - Testing the SDS011 from a Computer

  1. Install the CH34x driver to communicate with the sensor using the provided USB2TTL stick
  2. Use the Python script I shared here to verify the correct functioning of the sensor. Before running it make sure the serial port currently hardcoded to "/dev/cu.wchusbserial14210" is available for you, by running a "ls /dev/cu.*" or simply tab completion. If not copy paste there whatever looks similar to what I wrote. Sorry, I was too lazy to use string arguments to pass this value.


Step 2 - Setting up the esp8266-based MCU

  1. Install the SIS CP210x USB to UART driver to be able to detect the MCU from your PC
  2. Install the Arduino IDE, install the esp8266 extension (see example here). Select the right board (I am using NodeMCU 1.0) and make sure the port is also correctly set. I personally have something like "cu.SLAB_USBtoUART" since I installed the CP210 driver.
  3. Open the Blink example and flash it to the MCU to see if the cross-compilation chain worked correctly.
  4. One of the first things I tested was the WiFi Manager, which will also be useful later so make sure to set this up as well. This implements an access point and a basic captive portal to collect the SSID and password of the wifi network you want to connect, in case this was not done before or the network went down. Those data are then saved to the EEPROM so that the board can go directly online later on, for instance to collect and send samples to a remote server.


Step 3 - Connecting a LCD Display
There exist mainly two possibilities to connect to common HD44780 LCD displays (e.g. 16x2, 20x4, etc. ): i) using the GPIO and the library LiquidCrystal, a solution that I discourage since this will take 12 wires (see Fig. 1), and ii) using a standard bus such as I2C, which will allow for a more limited number of pins (4) as well as for reusing the same pins for other peripherals. Specifically, I got a I2C driver from Amazon which I soldered to the display as connected in the example in Fig.2.

Fig. 1: GPIO to connect to a Display

Fig. 2: I2C Display driver

As for the wiring, the driver has 4 pins: GND and Vcc, respectively connected to pin GND and Vin (+5V) of our MCU board, as well as the SCL connected to D1 and the SDA connected to D2.
Now the setup for the software side:
  1. Set up the LiquidCrystal_I2C Arduino library (if you are lazy follow this video)
  2. Flash one of the provided code examples to test the correct wiring and software setup

A very good tutorial for this part is reported here.

Step 4 - Connecting the SDS011 Dust sensor

I found this guide particularly useful to understand how to connect the sensor to my MCU board.
We basically connect the GND and +5V to the main board (GND and Vin, same as for the display) and the TXD and RXD of the sensor to the GPIO. If you notice on the usb stick shipped with the sensor, the TXD is connected to R, while RXD to T. As shown in the guide, connecting to R is enough to retrieve samples, though is advisable to also connect pin T to use all protocol features. Specifically, I connected R to my GPIO pin D6 and T to pin D7. The idea that makes serial communication possible on GPIO pins is that of SoftwareSerial which replicates the existing hardware UART.

As for the software side:

  1. Set up the SDS011 Arduino library from here, or one of its extensions (to the full protocol) from here or here
  2. Download and flash the code I shared here
  3. Wait and pray

Tada! You now have a low cost modular measurement system for PM data.


Feel free to add sensors for temperature or other pollutants. Shall you need it, here is a very good comparison of what you can do/buy. Also, mind that the Vin pin results from the connection of the MCU board with the USB cable. So shall you want to power everything with a battery at 3.3V, you might need a boost DC/DC converter to get your 5V.

I found out there has been lots of other people using basically the same hardware for the same purposes. So no need to reinvent the wheel, though it is always nicer to discover things yourself to learn. Here are a few links:
Have fun!

Andrea

No comments:

Post a Comment