Previous Blog |
---|
Project R.A.G. - Blog #4 - Quick Tool Change Mechanism For The Robot Arm |
Blog #5 - Sensor & Light Module
1. Introduction
Hi! This will be my fifth blog for the 1 Meter of Pi - Design Challenge. In the last blog, which you can find linked at the top of this blog, as well as on the bottom of it, I covered my mechanical design and build of a quick tool change mechanism for the robot arm. This will blog won't be about the robot, but rather about one of the modules that will a part of this project, the sensor & light module. As I've already mentioned in my third blog (Project R.A.G. - Blog #3 - Lift Off ), as part of the starter kit, we received the Enviro Hat mini, a great add on for the Raspberry, which can measure a lot of stuff, with some of them being, temperature, humidity, pressure, illumination. While this will do the job great, it's always good to have another set of sensors and compare the results, to have a bit of redundancy. If there is a big mismatch between them, some sensors are either faulty, or something strange is happening with our project. Besides the sensors, there is another thing this module will do and that is control the lights. Light is vital for all plants as we know, because, without, they can't perform photosynthesis, but more on that later. Let's talk about my general idea for this build and turn that into a plan and final product in the end.
2. Idea
The idea is of course making a module with the functionality that is described above. But, what do I mean by module? I want this to be an isolated part of this project contained within it's own enclosure with 2 cables running to it, one for power and one for communication. In this way, when the whole system is designed like this, it's much easier to swap out just this module than go and search for the fault in a nonmodular system. Another thing that is easy to do with this set up is turn off the power of the module using a relay for example when the project wants to go into some power saving or sleep mode. Let's take a look at what I would like to measure with this module as well as what kind of lights I want to control.
Sensors
Let's first take a look at the parameters that we want to monitor and from there we can determine the sensor we are going to use for this project. I've listed a few of the sensors and parameters that can we can monitor with the Enviro Hat Mini, but here's a list of everything we can do with that board:
- Temperature
- Pressure
- Humidity
- Light
- Proximity
- Noise
It's a pretty vast list for such a small board, but as I've talked in the Blog #3 - Lift Off, we won't be needing all of that, rather, we will be needing, temperature, pressure, humidity and light. So, the main goal would be to have all of those measurements, if possible, on the sensor module as well. Another thing I would like to add would be some kind of a gas module, to know if anything funny is happening, but I'll get more into that later. So the main goal for this box would be to track these:
- Temperature
- Pressure
- Humidity
- Light
- Gas
In the planning phase I'll go over what sensor I have on hand and what of these I will be able to cover with them, for now, let's focus on the next important thing of this module, and that is light. Let's see why is the light important for plants, and later see what kind of light we need and how will we control it.
Lights
Light is of course essential to the livelihood of plants, and the first term that comes to mind is photosynthesis of course. This is one of the most important cycles in nature, one, without which we wouldn't be able to live on this planet. To keep it short, photosynthesis is a process in which green plants and a small group of other organisms transform light energy into chemical energy essentially. The plants take in carbon dioxide, water, light and minerals, and as a produce we have oxygen and energy rich organic compounds. Seeing this we can conclude that light is more than important, specially considering how far and where we're sending out gardener, so an illumination system is a must.
The logical way for this project would be to go the route of using LED-s for lighting this project. We can get away with using halogen bulbs or LED bulbs for that matter, but that would mean we would have to have mains power running through the project, and honestly, I like avoiding that if it's not necessary or a better option. Instead of that, we can use 12V LED strips which we can easily control with a micro controller and control everything from how bright they are to the color of their light. I will be covering all of this in the planning phase.
3. Plan
With the main idea and goals set, we can now go to the planning phase, to take a look at the exact sensors we are going to use, the LED strips, and how we're going to use all of that. A good start here would be a diagram to show all the various parts of the module, which we can slowly cover one by one.
On the diagram above, we can see the basic layout for this module, as I'll be using 12V LED strips, I'll be getting 12V to the box, but rather than dragging another cable with 5V for the Arduino, I'll just use a small linear regulator (7805CV) to power the Arduino and various sensors, since they don't consume that much power. From there, we can see that we will have a data connection for the Raspberry which will be in the main box and the other peripherals I've already mentioned. As you can see, I will be using a MQ-135 air quality sensor, DHT22 for temperature & humidity and photoresistor for light. The only thing that I won't be able to cover from the list above will be the pressure sensor. Let's take a look at these sensors one by one.
Sensors I'm planning on using
{tabbedtable} Tab Label | Tab Content |
---|---|
MQ135 | MQ series of gas sensors use a small heater inside with an electro-chemical sensor. They are sensitive for a range of gasses. Here's a list of some of the sensors from their range, and what they are specifically used for:
And there are many more in this list, this is just an example of a few of them. The one I'll be using for this project will be the MQ-135 sensor. The output of this sensor will be an analog output which will serve as our data. The sensors can be calibrated if we have a known gas and there is also a small potentiometer on the module, so you can calibrate a digital output pin to trigger something so you can use it without a microcontroller. You can find a couple of relevant links for this sensor here: |
DHT22 | DHT22 is a great little sensor that combines temperature and humidity measurement into a great little package. It has a humidity measuring range of 0%-100% and a temperature measuring range of -40C - 125C, though of course, I won't be using in those sorts of temperatures. There is a library for the Arduino which is incredibly easy to use with this sensor, which makes it really popular, since it's widely available and easy to use. You can find a couple of relevant links for this sensor below, including the link where you can download the latest version of the library for this sensor: |
Phototransistor | To measure the illumination I will be using a phototransistor. It's a small component that looks like an LED that is sensitive to light. If you measure the resistance across it, you can see a significant drop of resistance when you shine a line into it. To use this, I will just connect it as a part of a voltage divider to one of the analog pins and measure the voltage. I can't find the exact one I am using, since it was part of an Arduino Kit. Here are a few links that can be of use when we are talking about phototransistors. |
LED strips
In the idea section I already had a short explanation of why light is essential for our project and that we will be using 12V LED strips, but before we go into how we can control the LED-s, let's first talk about what kind of light do plants need. There are a few parameters to look at here, how long is the illumination during the day, because plants don't like having light 24/7, how strong of a light do we need, stronger light is logically good, but if we go too strong, we can ruin the plants, and of course what spectrum. To we need UV lights, white lights, combination of red & blue and so on. The things I'm going to talk about now will be based on some articles I've read and I'll reference them below, I'm no expert on this topic, but this is the data I've gathered during my research. Of course, using what I have on hand, I'll try to get my execution to follow the path of the results of this analysis.
What kind of light do we need?
I've done my research for this part by reading around a lot of different articles on what kind of light do plants prefer and need. This is an extremely wide and complex topic, so I'll just present the limited information I managed to gather through my research. Reading around, I've came to a few places referencing that the most important part of the spectrum for plant growth are red and blue, but that is not to say, that the rest of the spectrum is useless. This explains why a lot of the LED grow lights are usually tinted in pink/purple. For photosynthesis, the red spectrum was the most important, followed by the blue, followed by the rest, but further studies suggested that a full spectrum white light, like something the plants are getting from the sun, has it's ups as well. Of course, there is also the question of how much light is good and how strong the light should be, this also varies from plant to plant. Since I am planning on using LED RGB strips but 3 pin connectors, I decided on a bit of a weird approach, where, instead of controlling all 3 channels, R, G & B, I will be controlling 2 channels, RB & G. In this way, I can have the purple light as well as the white light. While this might not be a perfect solution, it will certainly work. Again, this is a very wide topic, and professional grow lights cost how much they cost for a reason, there is some serious research done on all of the thigs I've talked about and more. If you are interested in some details, here are 2 from the bunch of sites that I went through, that talk about purple lights a bit:
- https://www.valoya.com/why-is-led-grow-light-purple/
- https://growlightinfo.com/why-are-grow-lights-purple/
Control circuit
I love using LED strips because they are incredibly easy to use and set up with any sorts of microcontroller. There are dedicated LED driver IC-s, but I found the way with using a small MOSFET for each channel worked great for me every time. The only thing to watch out for is the threshold voltage for the MOSFET as some microcontrollers work on 3.3V logic level like the ESP8266. This is something I have played around with already in my Ski Lamp Project. There is an easy way to overcome this problem by adding a small OpAmp before the MOSFET:
This time, instead of the IRF530 I will be using the IRL540N. I have a couple of them laying around, they are of course, as the IRF530 also is, N channel, and this MOSFET is rated up to 36A which is more than sufficient for our needs of running our LED strips. You can find more about this MOSFET as well as it's datasheet on this link: IRL540N. Looking at the datasheet, we can find the threshold voltages for this MOSFET:
We can see that the threshold voltage can be anywhere from 1 to 2V which makes these a great MOSFET for our application. It can be utilized without any problems with 3.3V logic microcontrollers such as the ESP8266 line or the Arduino MKR series, but it will work great as well for our Arduino Nano, since the Nano works at 5V. This means we can go without using the OpAmp stage and just use the MOSFET. A Youtuber by the name of GreatScott of who you certainly heard about before had an amazing video on this topic a few weeks ago, he talked about driving MOSFETS safely using microcontrollers. It's a really good educational video on this topic and here is the link to that video: https://www.youtube.com/watch?v=8swJ_Bnsgl4 . I highly recommend it. To summarize that video, let's see the bare circuit of how I would usually drive a MOSFET
This circuit proved to work great many times for me, and it works, but as he explained it in the video, it can cause harm to the microcontroller long term. There is a couple of ways around this of course, one would be to use a dedicated MOSFET driver IC, or to simply protect out microcontroller a bit with a few additional components. Here is how that circuit looks like:
Comparing the 2 circuits above, we can see that the second circuit has an additional 200Ohm resistor connected to the pin of the Arduino as well as 5.1V Zenner diode. When we are switching the MOSFET on without the 200Ohm resistor we having current spikes well above the microcontroller upper safe limit, and when we are using PWM to drive the MOSFET, we are having a lot of those spikes and we are essentially damaging the microcontroller faster in that way. In other words, we are using the 200Ohm resistor to limit the current. The zenner diode serves the purpose that is to block any overvoltages reaching our microcontroller pin. For a detailed explanation for all of this, I highly recommend the video. So for the driving circuit, I will be going with second one, since at the end, it's just 2 adtional components per MOSFET.
4. Design
With everything planned out we can finally start with designing and making the module. There are 3 parts to the design of this module:
- Mechanical - Enclosure
- Electrical - Schematic & Perfboard
- Software
I will be first covering the mechanical and electrical part of the build while I'll leave the software for last which will be used to tie everything together. Let's begin with the enclosure since that will determine the size and shape of the perfboard that we can use at the end.
Enclosure
Our enclosure will be a mix of off the shelf parts and custom designed 3D printed parts. The whole design could be just either of those, but I find that the combination of both gives the best results over all. I want it to look good and custom which is why I like 3D printing, but I don't want to waste filament on printing a simple box, something that can be purchased in a store with much better characteristics than a 3D printed one. To house everything in I will be using an electronics box (wiring box) with the size of 100x100x55mm.
For the rest of the build I will be using PLA printed on my Ender 3 Pro, specifically, I will be using Creality Yellow PLA, link for all of the 3D models that I will be using for this project you can find on the link here: Project RAG - 3D Models. Those models are provided as they are, and you are free to do with them whatever you want. You will find both STP files as well as STL files which are ready for printing. Let's get to designing the parts for this module, I'll link the specific for this build after we go through all of the parts.
{tabbedtable} Tab Label | Tab Content |
---|---|
MQ135 Mount | Let's start off with the mount for the MQ135 air quality sensor. This will be I decided to mount this on of the sides, since it will also contain the power on LED and indicator LED. The power on LED is self explanatory, while I want to have another LED for testing purposes to be as an indicator if everything is working and stuff like that. For example, if were detecting some crazy temperatures it can both send alarms to the Raspberry and flash this LED so we know there is something wrong with the sensor. On the pictures above you can see the top and bottom side, it's designed to be mounted using 6 M3 screws and either cutting the thread into the box or using a small nut on the side. I left 4 small holes for attaching the MQ135 sensor to the mount which align with the holes on the small module that I have. It's a really easy model to print, I printed in standard quality at 0.2mm layer height without any supports. The LED holders are designed for the standard 5mm LED-s. In my case they press fit really nicely in, so I didn't need to any glue to hold them in place. |
DTH22 Mount | This is the second mount for our sensors, this one will primarily, as the name suggests, hold the DTH22 sensor that we talked about, but will also hold the small photoresistor. The style of the mount will of course resemble the previous mount, since it's intended to just screw on to a plastic surface. These sensors will be mounted on the lid of the box, so that the light sensor will be able to read the light that is coming towards the box which will turned towards the center of our garden. Just like the last model, it is printed at 0.2mm layer height with no supports needed. It is designed to be mounted using 4 M3 screws and for the rest of the components to fit snuggly. One thing I had to do was to use a really fine drill bit to drill the lid of the box for the wires from both the DTH22 sensor and the photoresistor, but it turned out really good and clean like that. Here is the finished product. |
7 Segment Display Mount | The last thing that we need to mount are the 2 segment displays. I want to have one displaying temperature and the other one displaying humidity. While they aren't necessary for this build, specially because this is supposed to be automated without human touch, I like how it looks and it's a really easy way for troubleshooting and testing. I am using standard 4 digit 7 segment displays for this. The displays are designed to snap fit into place. This took a few tries, but I'm pretty happy with how it works now. But a bit of superglue won't hurt either here. Just like the previous ones, it was printed at standard settings at 0.2mm layer height with no additional supports. Here is how it turned out: |
Connectors | This is something I dedicated the whole blog to, and you can find it here: Project R.A.G. - Blog #2 - Strapped In For Launch - Connectors . I will be using those 3D printed connectors, both the 3.5mm version and the 5.5/2.1mm barrel jack version, but I also designed a new one in the same formfactor, a USB connector. Why a USB? My idea is to have an external port for reprogramming the Arduino inside so I don't have take apart the box every time I want to tweak the code a bit. Besides this USB connector, there are 3 more connectors on the enclosure, one with barrel jack used for the 12V input and 3 more 3.5mm connectors. One of those is used for I2C communication with the Raspberry, while the other 2 are used for connecting the LED strips. Link to the models for all of the connectors can be found here: 3D Printed Locking Connector. This is a really snug fit for the connector, but I would highly recommend adding a bit of super glue, since it will get loose overtime if we use it. Another great alternative to superglue would be a bit of hoy glue, but both should work excellently here. |
With that, we have all of the parts needed to assemble the enclosure for this module. All of the 3D models, both STP files and STL files can be found on this link here: Light & Sensor Module 3D Models. You are free to download them and modify them however you want to and do with them whatever you want to. The models are provided as they are and I can say they worked great with my printer but make sure the tolerances I used are okay with yours. Also, besides these models you will find a small model of the MQ135 sensor and the DTH22 sensor, these aren't super accurate, but can be useful for checking if the tolerances are okay. And if you're interested in the models for the connector, take a look at this link: 3D Printed Locking Connector. Here is how the finished enclosure turned out in the end:
Electronics
With the enclosure mostly completed, we can turn out attention towards the electronics inside. I thought at first that this box will be plenty big for a perfboard containing everything I need, but it turned out to be a bit more tricky than I thought. A custom made PCB would fit no problem, but since I need a lot of connectors I had to get creative a bit as I will show in this section now. I've already covered some of the electronics a bit in the talk about driving MOSFET-s using microcontrollers, I will now go over the full schematic and explain the different aspects of it.
Schematic
It's a really simple and straightforward schematic, there's just a lot of small components needed to be connected together. Here is the big picture for the electronics for this module.
On the picture above we can see an Arduino Nano in the center with a lot of different modules around it. The first module is of course the 7805IC which a small and cheap linear regulator. Rather than drag 2 cables, one for 5V and the other for 12V for this module, I decided to have only one power cable, the 12V one, and drop the voltage down to 5V with the 7805. Besides that, we have the before mentioned sensors I am going the use, the MOSFET stage for the LED strips, and 2 TM1637 display modules which will be showing the temperature and humidity that we are measuring using the DHT22 sensor. I've already covered a bit about the MOSFET stage, but there is the final schematic for that part:
I will be using 4 MOSFET-s in total and there's a few reasons for it. One would be that my connectors support 3 pins, because I didn't manage to find a female 3.5mm connector with 4 pins and the other one is that I will be using the RGB LED strips like they have 2 channels, one channel will be RB and the other one G. In that way I can have the violet light, as well as the white light when I use both channels at the same time. As described before, each, channel will have a 200Ohm current limiting resistor, a 5.1V Zenner diode for overvoltage protection as well as a 10K pull down resistor to make sure we always have a defined state on the MOSFET gate. One more thing to add that I forgot on the schematic above are the 2 indicator LED-s.
They are just connected to digital pins and to ground using a 220Ohm resistor. The green LED will be used as a power on indicator, while the red one can be used for testing out communication, showing that we have data transfer and so on. Of course, they will both be able to be used in that way, so we can have a visual representation and know if we've received data that the Arduino can understand, or if we can see that the Arduino is trying to send data to the Raspberry and so on. Here is a table with all of the connections to the Arduino:
Pin on the Arduino | What's it connected to: |
---|---|
2 | Display 1 - data |
3 | Display 2 - data |
4 | Clock signal for both displays |
5 | PWM Pin for the RB channels for the LED strip 1 |
6 | PWM Pin for the G channels for the LED strip 1 |
7 | Power On LED pin - Green LED |
8 | Indicator LED pin - Red LED |
9 | PWM Pin for the RB channels for the LED strip 2 |
10 | PWM Pin for the G channels for the LED strip 2 |
11 | DHT22 data pin |
A0 | MQ-135 data pin |
A3 | Phototransistor data pin |
A4,A5 | I2C communication |
Perfboard
As I've mentioned already, there isn't a lot of space for a perfboard once we put in the connector into their respected slots. To manage to fit the perfboard in, I had to get creative and do a small sandwich circuit. The top floor will have the pins for the Arduino Nano and few of the connector, while the the lower floor will have more connector, but also, the 7805IC and all of the MOSFET-s. Here is how the perboard turned out. For the connectors I will go with the small nylon connectors, I love using them because they are widely available and are crimp connectors.
On the pictures above, you can see the lower floor. On it we have the connectors for the 2 LED strips and 12V power. In the back, you can see the 4 MOSFETs that will be used for controlling the LED strips and you can also see the 7805 linear regulator. I've put the pull down resistor and zenner diode down here, while the current limiting resistor had to go to the other level since there wasn't enough space. The headers that are soldered on the side are used to connect the floors together, some of the pins are used so the 2 of them communicate, but most of them are there for structural integrity.
On these pictures, you can see the upper level. This level has the slot for the Arduino Nano in the middle and a lot of connectors around it. There are 2 connectors for the 7 segment displays, 1 for the Green LED, 1 for the Red LED, 1 for the MQ-135, 1 for the DHT22, 1 for the phototransistor and 1 for the I2C communication. This took a bit of time to solder, but I went slowly and for one of the first times, there weren't any mistakes that I had to go and repair further on. Here is how it looks stacked.
With that, there is only one thing left to do, and that is to pop in the Arduino Nano. This is by far the microcontroller I use the most, it has a lot of pins, costs less than 5$ and is such a great small size that I can really pack it into anything without any issue. It's a great board to be used in modules like this because of the mentioned things.
5. Final Build
With all of the 3D printing done as well as soldering, we can now put all of it together and finally program it and test it. Let's begin by putting together what we have for now so we have sensors connected to our board. Back to the soldering iron, we need to solder on some wires to all of the sensors and outside connectors and then crimp on the connectors for our board. The barrel jack connection doesn't need any other reinforcement since it has a nut and thread, but the 3.5mm connectors will need a bit of hot glue or super glue. I went with hot glue, since that usually works better in my opinion and I can also tidy the wires a bit.
Hardware
First things first, soldering all of the wires to the outside connectors, the LEDs and to all of the sensors and then crimping the connector pins on to those wires. I'm using wires from an automotive harness so they are pretty stiff, which you will probably notice from some of the upcoming pictures.
After I was done soldering on the wires, I went ahead and made small labels for all of the cables/connectors. You can also see those labels on the perfboard as well. There are a lot of wires here, mostly in just a few different colors that I had, so when we have all of them at one place, it can get a bit confusing with what is what. I found this way of making small labels out of sticky paper and a black marker to work great for me for projects like this. It makes the life much easier when I have to take it apart for some reason, or fix something and honestly in my opinion, looks cooler!
All we need now is the lid on top. One thing to mentioned that you might have spotted is that I haven't yet wired the USB connector for programming the Arduino. I don't currently have one of those connectors, or cables from which I can salvage the connector, so this is something I will do at a later date, but I left it there as an optional upgrade which I can do whenever I get the required USB Mini connector that the Arduino Nano uses.
Software
All that we have left now is to do some coding to get this module going. I will show the test code I wrote for testing out this module, the final code for this module will be a bit different since it doesn't use I2C communication in this first test, but will have to in the future so it can communicate with the Raspberry Pi in the main box. To get started with the box let's first take a look at the libraries needed for this build
Libraries
To use the code I am about to show, you will need to download and install 4 libraries for the Arduino. To install them, I downloaded the libraries as zip files and added them. If you haven't done this before, here is a great little tutorial on how to easily do that: https://www.arduino.cc/en/guide/libraries. Here is the list of the libraries we will need, as well as the links where you can get these libraries.
Name of the library | Link for downloading the library |
---|---|
DHT Sensor library | https://www.arduino.cc/reference/en/libraries/dht-sensor-library/ |
Adafruit Unified Sensor Driver | https://github.com/adafruit/Adafruit_Sensor |
MQUnifiedsensor library | https://www.arduino.cc/reference/en/libraries/mqunifiedsensor/ |
TM1637 display library | https://github.com/avishorp/TM1637 |
Most of the libraries that you can see in the table above are self explanatory by name, the only one that needs a bit of explanation is the Adafruit Unified Sensor Driver. I tried working with the DHT22 without using that library but I just got an error, so this library is required as well for everything to work.
Code examples I used
To make my final code, I went through the examples of each library and took the parts I needed from each of the examples. You will have the whole code below, but if you want to know where I got what part of code from where, here is a list of code examples I used for writing my short piece of test code:
- Examples → MQUnifiedsensor → MQ-135-ALL
- This example was used for all of the code that's about this sensor
- There is a calibration procedure that needs to be done with this sensor when you are first using it which is described in this example, so I highly recommend on checking out this example to see how to properly do that
- Examples → DHT sensor library → DHTtester
- I used this example to see all of the commands on how to read the data from the DHT sensor which proved to be incredibly simple with this library
- Examples → TM1637 → TM1637test
- This example does a great job of covering all of the possible functions we can use with this library, so I just went and took the functions I needed for this part of the project
Final code
I combined the things I needed from these examples and added the rest that was needed for driving the LEDs and ended up with a code whose purpose was to test out our module. To test out our module, this code needs to read the data from all of the sensors and control the LED strips. I wrote a few functions for reading all of the data, showing the data on the 7 segment displays as well as on the serial monitor. And a simple routine for controlling the LED strips.
This code will work for you too, but it might not be accurate for your MQ-135 sensor. This sensor requires a calibration procedure which measures and sets up a parameter that will further be used for converting the raw data from the sensor into sensible data that we can use. This calibration procedure is described in detail in the example code Examples - MQUnifiedsensor - MQ-135-ALL and you can also find a long and detailed description here: https://jayconsystems.com/blog/understanding-a-gas-sensor
The value I got in the end was 12.3, I don't know how accurate it is, but I followed the described procedure as closely as I could to get to this number
After you go through the calibration procedure you can go and copy that value into my code at the required place and use it like that. Let's take a look now at the final test code and finally play around a bit with this module to test it out.
/* * * SENSOR & LIGHT MODULE * * _____________________________________________________________________________________________________________________ * This code is for the sensor & light module featured in my project for 1 Meter of Pi design challenge on element14.com * You are free to do with this code whatever you want! * _____________________________________________________________________________________________________________________ * */ // Libraries #include <Arduino.h> #include <TM1637Display.h> #include "DHT.h" #include <MQUnifiedsensor.h> // Pins we are going to use #define PinDisp1Data 2 #define PinDisp2Data 3 #define PinDispClock 4 #define PinRB1 5 #define PinG1 6 #define PinGreenLED 7 #define PinRedLED 8 #define PinRB2 9 #define PinG2 10 #define PinDHT22Data 11 /* * Besides these, we have 2 sensors connected to the analog pins in this way: * MQ135 ---- A0 * DHT22 ---- A3 */ // Setting up the library objects // DHT22 #define DHTTYPE DHT22 DHT dht(PinDHT22Data, DHTTYPE); // TM1637 Displays TM1637Display display1(PinDispClock, PinDisp1Data); TM1637Display display2(PinDispClock, PinDisp2Data); // MQ135 Sensor #define placa "Arduino Nano" #define Voltage_Resolution 5 #define pin A0 //Analog input 0 of your arduino #define type "MQ-135" //MQ135 #define ADC_Bit_Resolution 10 // For arduino UNO/MEGA/NANO #define RatioMQ135CleanAir 3.6//RS / R0 = 3.6 ppm MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type); /* Exponential regression: GAS | a | b CO | 605.18 | -3.937 Alcohol | 77.255 | -3.18 CO2 | 110.47 | -2.862 Tolueno | 44.947 | -3.445 NH4 | 102.2 | -2.473 Acetona | 34.668 | -3.369 */ // Variables float temperature; float humidity; int illumination; float CO, Alcohol, CO2, Tolueno, NH4, Acetone; void setup() { // Setting up Serial Serial.begin(9600); Serial.println("Module has started"); //Setting up DHT22 dht.begin(); // Defining the pins for out indicator LEDs pinMode(PinRedLED, OUTPUT); pinMode(PinGreenLED, OUTPUT); // This is our power on indicator, meaning that we have power, and that the Arduino has gone through setup() digitalWrite(PinGreenLED, HIGH); // Testing our Red LED digitalWrite(PinRedLED, HIGH); delay(500); digitalWrite(PinRedLED, LOW); delay(500); digitalWrite(PinRedLED, HIGH); delay(500); digitalWrite(PinRedLED, LOW); delay(500); digitalWrite(PinRedLED, HIGH); delay(500); digitalWrite(PinRedLED, LOW); delay(500); //Display Setup display1.setBrightness(7); display2.setBrightness(7); // MQ135 Setup MQ135.setRegressionMethod(1); MQ135.init(); // There is a calibration procedure needed for the R0, which you can find in the example of the MQ library MQ135.setR0(13.02); Serial.println("This is a test code that will display all of the read data from the sensors."); Serial.println(""); delay(1000); } // This function is used for updating the data on the 4 digit 7 segment displays void UpdateDisplayData(){ display1.showNumberDec((int)temperature, false); display2.showNumberDec((int)humidity, false); } /* This function is used for reading all of the data we can acquire from the MQ135 sensor * This part of the code was taken from the example for the MQ library, if you plan on using this library, I highly suggest you check out * the original code, because there is a calibration method you should do once for every sensor. */ void ReadMQ135Data(){ MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin MQ135.setA(605.18); MQ135.setB(-3.937); // Configurate the ecuation values to get CO concentration CO = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ135.setA(77.255); MQ135.setB(-3.18); // Configurate the ecuation values to get Alcohol concentration Alcohol = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ135.setA(110.47); MQ135.setB(-2.862); // Configurate the ecuation values to get CO2 concentration CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ135.setA(44.947); MQ135.setB(-3.445); // Configurate the ecuation values to get Tolueno concentration Tolueno = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration NH4 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup MQ135.setA(34.668); MQ135.setB(-3.369); // Configurate the ecuation values to get Acetona concentration Acetone = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup } // This function is used for displaying the acquired data to the serial monitor of the Arduino void PrintDataToSerial(){ Serial.println("--------------------------------------------------"); Serial.println("Current Sensor readings:"); // This part will show our readings of temperature and humidity from the DHT22 // Temperature readings // I am reading the data in Celsius, you can easily also read in Fahrenheit, it is shown how to do that easily in the library Serial.print("Temperature: "); Serial.print(temperature); Serial.println("°C"); // Humidity readings Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%"); // This part will show our readings from the phototransistor Serial.print("Illumination: "); Serial.println(illumination); // This part will show our readings for various gasses from the MQ-135 sensor Serial.println("Air Quality: "); // CO readings Serial.print("\t CO: "); Serial.print(CO); Serial.println("ppm"); // Alcohol readings Serial.print("\t Alcohol: "); Serial.print(Alcohol); Serial.println("ppm"); // CO2 readings Serial.print("\t CO2: "); Serial.print(CO2); Serial.println("ppm"); // Tolueno readings Serial.print("\t Tolueno: "); Serial.print(Tolueno); Serial.println("ppm"); // NH4 readings Serial.print("\t NH4: "); Serial.print(NH4); Serial.println("ppm"); // Acetone readings Serial.print("\t Acetone: "); Serial.print(Acetone); Serial.println("ppm"); Serial.println("--------------------------------------------------"); Serial.println(""); } void TestLEDStrips(){ // Cycle through the brightness of white lights for both the strips for(int i = 0; i <= 255; i++){ analogWrite(PinRB1, i); analogWrite(PinG1, i); analogWrite(PinRB2, i); analogWrite(PinG2, i); delay(50); } delay(1000); analogWrite(PinRB1, 0); analogWrite(PinG1, 0); analogWrite(PinRB2, 0); analogWrite(PinG2, 0); // Cycle first the RB than switch and cycle green for(int i = 0; i <= 255; i++){ analogWrite(PinRB1, i); analogWrite(PinRB2, i); delay(50); } for(int i = 0; i <= 255; i++){ analogWrite(PinG1, i); analogWrite(PinG2, i); delay(50); } for(int i = 255; i >= 0; i--){ analogWrite(PinRB1, i); analogWrite(PinRB2, i); delay(50); } for(int i = 255; i >= 0; i--){ analogWrite(PinG1, i); analogWrite(PinG2, i); delay(50); } } void loop() { // This will read the voltage that is dependant on the illumination illumination = analogRead(A3); // The next 2 lines are for reading the values from the DHT22 sensor temperature = dht.readTemperature(); humidity = dht.readHumidity(); // Now we read the data from the MQ135 sensor ReadMQ135Data(); // Printing the data to the serial monitor PrintDataToSerial(); // Updating the data on the 4 digit 7 segment displays UpdateDisplayData(); // Testing the LED functionallity TestLEDStrips(); // A small time delay before we do our readings again delay(500); }
The code is simple and just goes through all of the point that we have mentioned, it reads the data from all of the sensors and using the functions I wrote displays it on the 7 segment displays and on the serial monitor. In the future I'll add a function for sending that data over I2C to the Raspberry. You can also see the function for testing the LED-s where I just go through the whole range of colors and lights that I can do with this module, this will of course be updated so it will control the lights in such a way that it responds to the commands that are sent from the Raspberry. Now, we can finally get to testing and see this module in action!
6. Testing
This is of course my favorite part of each project, or each step of the project, testing out what we have worked on and seeing if it actually performs as we planned or if we need to either do some repairs or start from the beginning. I usually do everything on a breadboard first, but to save time, I decided to just go with it, and I'm happy to say it worked out great! Here are the tests for the sensors and for the LED strips.
Sensor testing
Let's take a look at how readings look like on the Serial monitor when we are playing around with it.
One thing to note is that the MQ-135 sensor has a small heater inside and needs to heat up. The longer it heats up the more correct the values. For both the calibration procedure and the tests the sensor was preheated for about 30 minutes, while you can see in the data sheet and few other places that it's recommended to preheat it even for 24h, so my measurements might not be that accurate.
It's winter here, so here is how it looks like when I put the module near the open window in my room, it showed a temperature of 6.8 degrees and high humidity, while we can see that air was really clear. While the ppm values aren't absolutely correct, we can relatively compare them to their values in different situations. To get the absolute values correct, we need to do the calibration procedure really carefully.
On this picture you can see the values after I put a lighter underneath the sensor. The sensor reacted incredibly quickly with really high values as you can see, so, even though the absolute values aren't correct yet, comparing it to the values we had from our clear air test we can see a significant difference. This sensor also shouldn't be used at extreme temperatures (MQ-135), it's intended purpose is for room temperatures. Besides the lighter, I've also tried a bottle of 70% alcohol to which the sensor also reacted swiftly. On this picture you can also see the illumination value is at 264, this is the raw reading on that pin.
If we shine a light directly into it, we can see a significant difference at the illumination value. I don't need to convert this data to any actual data, since this raw reading will be enough for my purposes. Through testing I'll find what kind of raw value will work as a turn on/off point for me, I'll also look to see how this value compares to the value from the Enviro Hat Mini. Through the tests above we can see that all of the sensors are working as expected, the only thing we didn't look at are the 4 digit 7 segment displays. So here is how that looks.
I am really happy with how the module turned out in the end, I love the look of the 2 7 segment displays on the front, while they weren't needed for the project to function I think they are an addition that makes this module look just a tiny but cooler. With that, all of sensors and displays are tested and working, the only thing left are the LED strips!
LED testing
As I've mentioned before, I'll be using a 12V LED RGB strip for lighting this project. This allows me to change the color of the light as well as the brightness easily, also, since I'm using LED-s the power consumption will be kept pretty low. Instead of being able to control all 3 channels (R, G & B), I will be able to control 2 channels, I paired them in such a way that I can have control over RB and over G channel for both LED strips. To demonstrate this working, here is a short video showing the function I wrote for going through the range of this LED strip.
Sorry that the video is a bit buggy, it has a bit of a problem with focusing and adjusting to changing light, specially with the different colors of the LED strip as it went through the whole range. To try and capture the colors a bit better, I took some still photos with my phone, though, the colors don't show that good again, but still better than on the video.
On these pictures above you can see the colors a bit better that we have at the top of the range for both channels, with the RB being on the left of course and the G being on the right. Combining these 2 we can get close to white light since we will have the whole spectrum covered in that way. Here is how that looks at full brightness.
I just want to take a moment for a small warning when playing around with LED strips like this. This is something I've mentioned in one of my previous projects, but be careful when you are using a full reel of LEDs like this for testing. You can do some testing like this for sure, but, even though the LEDs don't consume that much power, they will heat up over time, they will heat up a lot. Last time I did this for a longer period of time, the plastic reel started to flex and deform from the heat.
The temperature got over 130C as you can see and it would continue to go higher if I didn't stop it, the only thing that gave it away was the slight smell of molten plastic, so please be careful if you are testing LED-s like this, or if you don't wanna deal with this, just cut off a short strip!
7. Future development & ideas
The software that has been written now is a test software to get all of the sensors up and going as well as all of the LED-s and LED displays. This is one thing that will certainly change throughout this project, or more accurate, it will change very soon, when I start playing with I2C to establish communication between this module and the Raspberry. As for the hardware, I won't be changing it any further for this project, but there are some things I wish I had done, one was a low power mode. This was already a small space where I had to stack 2 perboards one on top of the other to fit, so I didn't really have any extra room, but I had an idea of adding a small relay to the circuit, so it can turn the power on and off for the MQ135 sensors as well as displays for example. Besides that, I'm pretty happy with how it turned out and it does the job it's intended to do great!
8. Fun news from the ISS
For this segment of Fun news from the ISS, I didn't manage to find any real news based around gardening that were recent, but I did manage to find some cool and interesting facts regarding the Advanced Plant Habitat and the radishes they are growing there. Because astronauts need to take leaf samples and need to keep tabs on which plants they have taken samples of, they found the easiest way was to punch holes in the leaves. This is something I saw people ask many times when I was reading about the plant habitat.
9. What's next?
With the first module complete, my new priority will be doing a bit of software, I want to establish an easy way to communicate between the Raspberry and the various modules. As it's apparent from how I wired this module and how I've talked about in the previous blog, my choice for the communication protocol will be I2C. So I want to play around with it a bit and get to a level where I can easily add new modules without a lot of work, and doing research on this topic, looks like that it won't be that big of a problem whatsoever. Since Raspberry is finally getting put to use, I'm finishing up the main box which will contain the PDU as well. Besides that, the next big thing is the mechanical build for this project, which I will cover here shortly.
10. Summary
I'm really happy with the end result of this build. It covers a wide range of parameters that I can monitor with it and use further in my project as well as an essential tool for controlling the light which is essential for the plants to not just grow but survive as well. As I've said already, the only thing I'll be modifying on it will be the software so it can communicate with the Raspberry and adding the USB cable so I don't have to take it apart when I want to reprogram it, but rather use the outside port. Talking about that outside port, since it won't be used unless I am changing the software, I will print a small screw on cap, which will protect the connect inside from dirt and moisture while not in use. All of the relevant links for this competition, link to the next and previous blog as well as my GitHub where I'm uploading all of the models can be found underneath. Thanks for reading the blog, hope you liked it and found it interesting!
Milos
Relevant links for the competition:
Link to my GitHub where you can find all of the files used for this project (code, 3D models,...):
Link to my Project Collection:
Previous Blog | Next Blog |
---|---|
Project R.A.G. - Blog #4 - Quick Tool Change Mechanism For The Robot Arm | Project R.A.G. - Blog #6 - Mechanical Build P1 - Robot On Rails |
Top Comments