The Smart Gardening system project was my attempt at exploring IoT and edge computing with Lego to see what I can create from scratch. Shortly after the project went live, I realised that there are a limited number of analog pins available on the Arduino that I can use with analog sensors. It inspired me to create my own sensor hub to expand the number of available pins.
You might be thinking I could always buy such hub off the shelf if I search hard enough. Yes, I could do that but that’s not what I want to do. I also wanted to learn more about electronics and how they can be put together to achieve something. It’s like learning the basics for me and is best done by actually doing it. Just as with programming, you can’t get around learning and applying the fundamentals such as control flow, functions, classes and variables if you want to be good at it.
There are many approach to this.
You can start with designing the circuit first in a CAD software such as Autodesk Eagle, Fritzing or EasyEDA. However, these are paid software. If you are a hobbyist, the price tag might not be something you want to swallow. There is a free and open source version called Kicad, which is something that I used to design my circuit.
For me, my preference is to do exploratory prototyping straight on the breadboard. With exploratory prototyping, I get to see for myself if an idea work or if my understanding is correct in real time. If there’s a gap in my knowledge, I went online to learn the concept and then immediately apply it.
Preparing the Multiplexer
For this hub to work, we will need an analog multiplexer instead of a digital one.
Sensors such as the soil moisture sensor output its reading in the form of variable voltage, which is a type of analog signal. Depending on the conductivity of the soil (based on the amount of water), the voltage passing through will be different. This voltage level is then passed to a reader such as a multimeter or in our case, an Arduino.
Note: If a digital multiplexer is use, the variable output of the soil moisture sensor will inadvertently be converted to either a “1” or “0”, which is not what we want if we want to determine how much moisture is in the soil.
The multiplexer that we will be using for this project is the 74HCT4052D. This is a 4:1 multiplexer/demultiplexer with two sets of four independent input/output, a pair of common input/output and two select inputs.
I made the mistake of getting the surface-mount version (SMD) instead of dual-inline package (DIP). The former is smaller and does not quite fit on a breadboard. However, I did get a SMD to DIP adapter. It was part of a box of DIP sockets that I got because I didn’t want to solder the chips themselves directly onto the strip boards.
The SMD adapter comes with two side: One for SOP (or SOIC) and another for TSOP. The image below shows the SOP side and it is what we will use.
Note: SOP is short for small-outline package. Sometimes it also abbreviated as SO.
First, we will solder some header pins to it. Place the pins onto a breadboard for support and place the adapter on top, letting the side holes through the pins.
Use a lead-free solder with flux and start soldering away at 350 degree celsius.
Note: To any critic out there, I’m aware that my soldering job could be better. The flux is all over the place.
Place a multiplexer on top of the adapter and ensure the pins are aligned with the solder pads.
Quick Tip: Since the chip is so small, it can be difficult solder with traditional soldering iron and solder wire.
Before you get started, you could solder one of the connection point first. Then, place and align the chip on top before using the solder iron to reflow the solder. Use the smallest tip possible and only apply heating for 1 second. Any longer and you might risk destroying the chip. The chip should “snap” into place. Let the solder to cool and it will provide some sort of an anchor for you to finish the soldering job.
Alternatively, you could always get solder paste and apply them on all the points before placing the chip on top. The paste should provide a little bit of “adhesion”. Then use a heat gun and blow 350 degree celsius hot air to get the solder to melt and solidify.
Beware that you can destroy the chip much more easily with the heat gun than the solder iron if you leave it blowing on the chip for too long
Then, it was soldering time. Before I came across the above tips, I used the smallest possible solder iron tip I’ve got, gently apply the solder and soldering iron to the first pin. Once the first pin is secure, I went on to do the rest.
Here is the final result. And yes, it could be better with more practice.
Once the multiplexer is done, it was time for actual testing and prototyping.
Initial Exploratory Prototyping
The multiplexer is placed on a breadboard that will facilitate our testing. A breadboard power supply unit is added to provide us with 5v supply.
Based on the datasheet provided here, we can easily identify the different pins and their purpose:
- Pin 16 and 8 are VCC and GND respectively.
- Pin 6 is the chip enable pin and is active low.
As for the input and output of the multiplexer, the pins are as follows (The order they are listed refers to their logical position):
- Pin 12, 14, 15 and 11 are the 1st set of independent input/output pins. (They are also known as 1Y0, 1Y1, 1Y2 and 1Y3 respectively.)
- Pin 1, 5, 2 and 4 are the 2nd set of independent input/output pins. (They are also known as 2Y0, 2Y1, 2Y2 and 2Y3 respectively.)
- Pin 3 and 13 are common input/output pins. (They are also known as 2Z and 1Z respectively)
- Pin 9 and 10 are select pins.
To test and validate my understanding of the multiplexer, I used LEDs and connect them to pin 12, 14, 15 and 11. Then, pin 13 is pulled high by connecting it to VCC.
The rationale is that if there is a constant input through the common input pin, the right LED should light up when I manipulate the select pins. The LEDs are also going to be part of the final build to provide a way to indicate to the user (me) which sensor the hub has activated.
This was when I thought of how do I programmatically disable or enable the chip. Given that the chip is enable when pin 6 is low/grounded, this give me an idea of using a combination of resistor, a toggle switch, some wires and a NPN transistor to simulate a programmable on/off switch. And yes, you might be thinking I could use one of the Arduino pins to turn the chip on/off. I didn’t go with that route because I wanted to minimise confusion during programming. I want to send a “1” to indicate turn on and “0” to turn off. Having the extra components allow me to invert the active low requirement.
I connected the transistor’s collector to VCC. The transistor base is connected to a slide switch, which will be used to either turn the transistor on or off. The emitter is connected pin 6 and ground via a resistor.
Below is a video which demonstrated a combined testing of the chip enable and LED select operation.
Now that we have validated our understanding, it’s time to start looking at how we want to route the sensor’s signal back to the host.
Since at most only one sensor is active at any point in time, we could connect all the sensors to a common point and pass that on. I thought of two options. The first is to use diodes to create diode logic. The other was to use an OR gate.
A quick research show that diode logic have one major issue. They can cause the voltage level to drop. In the case of a soil moisture sensor, that return voltage is what we need in order to determine the soil moisture level. We can’t have it dropping. Therefore, the OR gate was selected. In hindsight, it proved to be a bad choice but more on that later.
Below is an image showing how I tested the OR gate. I hooked up one of the output pin to pin 1 on the OR gate. Since I’m going to support up to four sensors, I will need a few dual-input OR gates to consolidate the return signal. The rest of the OR gate input are hooked up to VCC. A LED is added on the “final” output of the OR to simulate reading the sensor value.
Here is the video showing how I tested and validated the OR gate solution.
Circuit Design using Kicad
Now that we have a good idea of how we want to do the circuitry, it’s time to put it down in a schematic. It is also my way of formalising the design.
I spent about 6 hours learning how to use the tool (Kicad) and designing the circuit. While designing this circuit, I watched some videos recommended by YouTube on circuitry. This was when I realised I needed some bypass capacitors.
Shortly after this, I realised that the OR gate does not work at all. The OR gate is a digital device whereas the multiplexer and the sensors are analog. This means the OR gate will effectively convert the variable voltage into either “0” or “1”.
And that is not what we want.
Upon realising the OR gate won’t work, it was time to make some modification to the circuit.
After much thinking, another multiplexer is deemed the best choice. This time, instead of connecting the common input/output to VCC, it will be connected to a screw terminal or a header pin so that we can pass the signal back to the host.
The 1Yn pins will be used to receive the sensor values and based on the select pins, we will pass the corresponding signal on.
After finishing the above design, I went ahead and prepare another multiplexer. Since the initial breadboard is too small for me to do any meaningful test, I moved the original prototype to a larger breadboard and started wiring up the two multiplexers together.
Despite the datasheet indicating the 1Yn pins are independent, I couldn’t get the output LED to light up even after selecting and passing the output from the first multiplexer in to 1Y0 pin. I even tried all 4 pins independently. The output LED connected to 1Z refuses to light.
Then, I decided to connect the remaining three pins (1Y1, 1Y2 and 1Y3) to VCC, pulling them high. This time, the output LED lighted up. I repeat the test on the remaining three pins by switching the connection to VCC around. This tell me that all 1Yn pins needed power in order for the multiplexer to output anything on the Z1 pin.
So this bring us to an updated design. We will need supply power to the pins and also allow the sensor value to pass through. This is where an inverter will come into play.
Since only one sensor will be powered up at any given time, that signal from the first multiplexer should also be used to turn off the VCC supply to the corresponding pin on the second multiplexer.
And so this is what we end up with. Although I have an inverter IC for prototyping, I can’t use it as it is also a SOIC/SOP and the IC has only 14 pins. The SOP adapter that I have are either for 8-pin or 16-pin. I did not want to waste the SOP adapter since I only have 2 left and that means I have to make an inverter by hand.
To do it, we will need a NPN transistor, a few resistors and some wires. Also, I got myself another soil moisture sensor for testing purpose.
Once I powered up the circuit, I dipped the sensor probes into the water. Then, I use a multimeter to check the signal the sensor returned. The value was around 2.8 volt. When the sensor is dry, it returned a 0 volt. This further validated my understanding and idea.
The schematic is then updated again.
With this updated design, it is finally time to actually implement it. And that will be for another day.