Using Teensy 4.1 Module
Often, the mobility of a system can completely change the scope of a technology’s functionality. That’s why drones provide such a powerful solution. Learn how this University of Calgary student built a compact data acquisition system mounted on a drone. Using a variety of sensors, the system is able to measure greenhouse gas emissions from oil refineries and farms.
The idea for this project began as a research proposal to create a modular and compact data acquisition (DAQ) system that could be mounted on a drone. The purpose was to measure greenhouse gas (GHG) concentrations above oil refineries and farmlands, to provide valuable data that could help improve chemical and agricultural processes. The prototype DAQ system described here can measure concentrations of carbon dioxide, carbon monoxide, methane, ammonia and ozone, as well as various environmental conditions, such as pressure, humidity and temperature.
The primary constraints for this project were to make it low cost and low power so that it could be manufactured easily and last for long-duration measurement tests. As shown in Figure 1, the current prototype uses two perfboards sandwiched together using pin-headers to reduce wire clutter. A future iteration, however, would most likely be manufactured on a PCB and use JST or Molex connectors to make the sensors more modular. This would be much more practical for a drone, since the main DAQ processing unit could be inside the fuselage, and the sensors could be mounted on the wing inside a ram-air scoop (Figure 2).
I reviewed many research publications on GHGs, to determine what gases to measure and what type of sensors were used to measure these gases. I spent a few days researching various sensors required for the project, and compiled all of them into an Excel spreadsheet, where it was easy to make side-by-side comparisons. In this article, an abbreviated version of that spreadsheet information is displayed in Table 1. As shown in that table, I had well over a dozen different sensors to choose from. A web version of Table 1 is included on Circuit Cellar’s article materials webpage. The web version includes links to webpages of each sensor product. The web version of Table 1 also lists many more specs for these sensors. These specs include measurement accuracy, measurement range and measurement rate specs. Also included are voltage input, peak current, operating temperature and comms protocol for each sensor. From the Circuit Cellar code and files webpage, you can download my full original Excel spreadsheet .
One interesting side note: most of the sensors that met the constraint conditions were for measuring carbon dioxide, whereas the choices of sensors for measuring more discrete gases, such as ozone or ammonia, were fairly limited, most likely because these gases are more niche to industry and not for commercial use.
The “brain” of the system is PJRC’s Teensy 4.1 Arduino-based microcontroller board, which has a built-in SD card reader and has a nice, compact footprint. My DAQ system uses four Winsen sensors (MQ-9B for carbon monoxide, MQ-137 for ammonia, MQ-4 for methane and MQ-131 for ozone), one Sensirion SCD30 carbon dioxide sensor with integrated temperature and humidity sensor and a SparkFun MPL115A barometric pressure breakout board. All of the non-gas sensors that I considered using are available from SparkFun. Table 2 lists these SparkFun sensors and their prices. Ultimately, I only ended up using the barometric pressure sensor and not the others for simplicity. On Circuit Cellar’s article materials webpage, a web version of this table has links to product pages for each sensor.
For power, I used a Pololu 5V 2.5A Step-Down regulator (D24V22F5) to power the four Winsen sensors, the Teensy 4.1 module and the barometer. A Pololu 3.3V step-up/step-down regulator (S7V8F3) is used to power the SCD30 sensor. These regulators were found on the X2 Robotics website and are available in most electronics shops. I used two regulators because the Winsen MQ sensor datasheets mention that the heaters inside the sensors can use about 350mW, so I was unsure about the current consumption in the beginning. After building the prototype and measuring the current draw, the entire DAQ system only used around 600mA, which means it needed just the 5V 2.5A Pololu regulator.
The Winsen MQ sensors are inexpensive and have a good concentration range. That said, they are analog and use semiconductor technology rather than infrared like those on the SCD30 to measure changes in concentration, and I had no precise way of calibrating them. Online, I found many ways of calibrating the sensors, but most methods were ambiguous or required known concentrations of test gases—which I did not have. So, I calibrated the Winsen MQ sensors my own way. I simply recreated the graphs given in the datasheet (Figure 3) , and used Excel to generate an exponential equation from the curve (Figure 4), which I later used to calculate each sensor’s internal resistance.
In contrast, the Sensirion SCD30 sensor is completely digital and comes pre-calibrated from the factory. For more accurate measurements, initial conditions can be sent to it through I2C, and it is recommended to leave the sensor outside in nominal weather conditions for 1 hour every day for 1 week to optimize measurements in the ambient environment. All other generic electronic components—such as buttons, LEDs, resistors and wires—were purchased through either Amazon or SparkFun.
As mentioned earlier, what’s convenient about digital and widely supported sensors is that they are easy to calibrate and the documentation is good. However, the four Winsen MQ sensors are analog, and though the datasheets provide enough information to use the sensors, a lot of the information was ambiguous, and similar projects were quite hard to follow.
Instead, I decided to go with a rather unorthodox way of reading somewhat respectable values from each of the sensors—to convert the datasheet measurements into Excel graphs, as shown in Figure 4. This way, I was able to automatically derive an exponential function for that curve, and use that equation to determine the concentration value given by each of the sensors. My MQ sensor calibration Excel spreadsheet is available for download from Circuit Cellar’s code and files webpage .
As a side note, the MQ-9B and MQ-4 can measure more than one gas, so it was important to graph the curve for the gas that I was interested in. Moreover, because they can measure multiple gases, there might be much higher inaccuracy in the measurements, since changes in other gas concentrations can potentially have a significant effect on the desired measurements. Additionally, some of the sensors are rated for measurement above 300ppm or even 400ppm, so while these sensors are not intended for lower concentration ranges—commonly used as alarms rather than accurate sensors—they still managed to provide reasonable data. For the next iteration of this project, I would use sensors from alternative manufacturers, but they would most likely be much more expensive than the ones chosen for this project.
To determine each sensor’s internal resistance (used for calculating the concentration), you must use a voltage-divider circuit and measure the voltage between the sensor output and the recommended resistor value. Say we have a basic circuit, with a power source and two resistors. Now, if we consider the internal resistance of the sensor to be one of the resistors, and a fixed value resistor such as 4.7kΩ resistor, we can determine the voltage between the two using the voltage divider equation. Since we know the voltage (5V) and the resistance of the fixed-value resistor, we can determine the internal resistance of the sensor.
Since the logic of the output is still 5V, which is not so friendly for the Teensy 4.1 analog inputs, I added more resistors to convert the 5V logic into 3.3V logic. The way I selected my resistors was simply by finding the proper combination that would all add up to the recommended single resistor value, while still converting 5V to 3.3V (with some error). For example, if the recommended resistor were 4.7kΩ, I would select a 1kΩ, a 670Ω, and a 3.3kΩ resistor. I did this for each Winsen sensor, tested each analog reading and also measured the output voltage, using my multimeter to make sure the values were correct.
The Winsen sensors use changes in internal resistance to measure gas concentrations. They use the ratio between the resistance of the sensor in a target gas (RS) and the resistance of the sensor in fresh air (R0) to calculate the concentration. Each datasheet provides a value for that ratio in fresh air, which can be used to determine the value of R0 for the sensor. (For all four, the ratio is 1.) The calculation method for determining R0 is reviewed more in-depth in the code section. Using the value of R0, we can then use the Excel equations derived earlier to determine the concentration measurement of the sensor after measuring the sensor’s RS value.
DESIGN PROCESS AND PROTOTYPE
The schematic in Figure 5 shows a modular design that is more suitable for drone applications. It shows the main board with the Teensy 4.1, alongside a voltage regulator and barometer and then the rest of sensors have connectors that can be routed to the wings of the drone and mounted into a ram-air scoop (Figure 2). The prototype design differs from the original schematic, since it combines all of the components into a compact sandwiched design for easy debugging and measurement testing.
As shown in Figure 6 and Figure 7, the prototype DAQ comprises two perfboards sandwiched together, using header-pins to easily access the bottom section. Figure 6 shows the reverse side of both boards, showing all of the wiring and hot-glue for strain relief. Shown in Figure 7, the bottom layer board comprises the Teensy 4.1, both Pololu regulators, and the MPL115A barometric pressure sensors.
The top layer board (Figure 7) comprises the four Winsen MQ sensors and the Sensirion SCD sensor. In addition, the voltage-divider resistors are mounted directly beside each respective Winsen MQ sensor for easy wiring. There are also three LEDs—one red, one green, and one blue—to show the states of the DAQ (discussed in the next section “CODE”), which indicates that measurements are actually being made. The additional wiring beneath each perfboard is hot-glued to relieve strain.
Because the Teensy 4.1 is Arduino-based, it was simple to set up a reliable DAQ system. As mentioned earlier, the documentation for the SCD30 is good, and SparkFun even made an Arduino library for it to make it even simpler. The library includes methods to configure and read data being sent from the sensor.
In my code, I set a few configuration settings, such as the measurement interval (2 seconds), the altitude compensation, ambient pressure and temperature offset (2°C just for error). I also used the Arduino ezButton library, which allows me to easily set up debouncing for the push button installed on the top layer of the prototype board.
To keep track of time, I used an unsigned long variable to store the current time in milliseconds, which should last up to 50 days until it overflows. As of right now, I don’t think we have any drones that can fly for longer than 50 days.
I defined a simple Boolean variable,
ledState, which is originally set to LOW—meaning not recording—and becomes HIGH after the button is pressed. While
ledState is HIGH, every 2,000ms (2 seconds), it will open the file named datafile, and write to it using the print method and then close the file. This keeps repeating itself until the button is pressed (resulting in
ledState LOW), or the SD is removed while the
ledState is HIGH and datafile cannot be opened within the loop.
The DAQ can be in four states, indicated by the LEDs: 1) not logging (solid red, all others off); 2) logging (solid green, flashing blue, red off); 3) SD card error (all flashing) and 4) one exception state, when the O3 sensor is not logging, but the rest of the system is logging (solid red, solid green, flashing blue). The SD card is FAT32 formatted using the built-in Windows quick formatter, and the file name created is BARO.txt.
Once all the data has been successfully recorded onto the SD card, you can import the SD card’s contents into a MATLAB script I created. It plots the information for all eight sensors with respect to time, which is formatted into hours, minutes and seconds (hh:mm:ss). The script has two functions:
dataGraph, which plots the graphs and automatically creates the titles and Y-axis formatting; and
timeCorrect, which duplicates each value in the raw data arrays to double the size of the array.
The data from BARO.txt is imported into Mathwork’s MATLAB using the Import Data function on the Home tab. The information is delimited by commas, and the Output Type is set to column vectors. Since the title for each data value was already named in set-up in the Arduino code, MATLAB automatically names each vector with the name of the column title. These vectors can then be imported by clicking the Import Selection (assuming all the columns are highlighted in blue). The entire import setup is shown in Figure 8.
After the data has been imported successfully, the
main.m function can be run, which will create eight graphs shown in Figure 9. If the graphs are very jumpy and it is difficult to discern a pattern, then the
average_scale value can be increased to smooth out the graphs. My DAQ code and my MATLAB code are available for download from Circuit Cellar’s code and files webpage . Figure 10 shows me (Peter) operating the drone’s controller.
Although this project was a success in that the final DAQ system recorded data from each sensor, there were some flaws. For example, to calibrate and test the sensitivity of the Winsen MQ sensors properly, it would have been expected to test them in a controlled environment (for instance, a bottle of methane containing a known concentration), or to use more expensive pre-calibrated digital sensors. Moreover, the Winsen sensors are more specifically targeted for when very high concentrations of a specific gas are required to sound an alarm, rather than to make precise measurements. For example, the MQ-137 sensor measures 5ppm-500ppm of ammonia, the MQ-131 sensor measures 10-1,000ppb of ozone, the MQ-4 measures 300-1,000ppm of methane and the MQ-9B measures 10-500ppm of carbon monoxide.
The project objective was to measure from at least 0ppm to several hundred ppb or ppm, depending on the target gas. But the Winsen sensors don’t offer this type of precision, so the measurements below the recommended or marketed detection ranges may or may not be accurate at all. In contrast, the Sensirion SCD30 with an infrared CO2 sensor measures from ambient CO2 levels (around 400ppm is the world average) to 40,000ppm with high accuracy.
This was clearly a flaw with the analog sensors selected for this project, and it should be addressed when designing the next prototype DAQ, while still trying to maintain low-cost and low-power constraints. Moreover, with the current sensors within a low-cost budget, the idea of using them on drones would only be practical within warmer seasons, since these selected sensors are not recommended for use at temperatures below 0°C. Many modifications must be done to this project to meet the final goal. However, the proof of concept and the prototype suggest that this is indeed a valid project and has potential for drones and measuring discrete gases.
 Circuit Cellar article code and files webpage
 Winsen MQ-137 Ammonia Gas Sensor manual
This article includes an APPENDIX with some Tables. Check it out on Circuit Cellar’s Article Materials webpage for the August issue.
CO2Meter.com | www.co2meter.com
DF Robot | www.dfrobot.com
Euro-Gas Management Services | www.euro-gasman.com
Gas Sensing Solutions | www.gassensing.co.uk
Mathworks | www.mathworks.com
MikroElektronika (Mikroe) | www.mikroe.com
National Control Devices (NCD) | www.ncd.io
PJRC | www.pjrc.com
Pololu | www.pololu.com
Senseair | www.senseair.com
Sensirion | www.sensirion.com
Sparkfun Electronics | www.sparkfun.com
SPEC Sensors | www.spec-sensors.com
Winsen | www.winsen-sensor.com
X2 Robotics | www.x2robotics.ca
IN CIRCUIT CELLAR MAGAZINE • AUGUST 2021 #373 – Get a PDF of the issueSponsor this Article