using a MUX shield II and a 5v 16ch relay

Greetings to the Arduino fam,

As a newcomer to modifying and tinkering micro-controlers i would like to share my problem with the community and get some ideas or tips on this one.

Im having an Arduino Uno which i want to get readings from 16 soil humidity sensors when i command to**, while i can also manually turn on/off 16 12v micro liquid pumps for a gardening system. Finally the whole system has to be connected to the internet through the RasPi 3 B which is already comes with WiFi on it. That means i need to code a GUI through Tkinter (or else) which sounds to be the easiest method all over the internet (but i still don't have any success by using it following the first tutorial).

Till now i have manage to run this system with one pump, which is getting power from Vin pin on Arduino, as for a relay im using a 240ohm resistor and a 2N2222 transistor to make a switch and a N4001 diode on pump's pins to kill any "spikes". (probably soon i will post scheme etc.) Finally the transistor is grounded to the Arduino GND.

One soil hum. sensor, w. fixed code which let me get continuous measurements (2sec) from a DHT11 RH/temp. sensor, **but i choose to command my soil humidity sensor when to get readings because cheap soil sensors are the worst quality, means that, as electricity runs through them to get the electric conductivity measurements, electrolysis destroying the probes. So i found two ways to control that, by letting the arduino counting(which proved that burned my mind while i was trying to include more commands and actions) or w. serial commands.

(*all readings are printed on serial port so i can get the readings to my pc or RasPi)

Then its time to get all the readings on my screen and by looking at the measurements, to start or not the water pump. The pump is coded to start for 5 seconds after my command and stop immediately, that is to prevent possible bug which may cause flooding (this can be change based on each installation e.g. pot size, growing medium etc.).

Well the whole thing is that i want to expand this automation for more plants.

I brought from a local store a 5V 16-Channel Relay Module for Arduino

SPECS

Working voltage: 5V
Channel: 16 channel
This relay module is 5V active low.
It is an 16-channel relay interface board, which can be controlled directly by a wide range of microcontrollers such as Arduino, AVR, PIC, ARM, PLC, etc.
It is also able to control various appliances and other equipments with large current.
Relay output maximum contact is AC250V 10A and DC30V 10A.
Standard interface can be directly connected with microcontrollers.
Red working status indicator lights are conducive to the safe use.
Widely used for all MCU control, industrial sector, PLC control, smart home control.

The next days I'm going to get a Mux Shield II from Mayhew Labs, a shield which seems to let me expand the analogue reading pins to 16 (which connected to the soil hum. sensors). Im also thinking that i can use an Arduino nano (that i already have) which hold some analogue pins that can do the job, but this is up for discussion.

At the end I'm coming up with some questions

-How to connect the relay module?
-Do i really need the Mux shield?(i need opinions)
-Is there any useful thread about GUI which running on Raspbian and allowing control over the arduino? i tried with pyfirmata but no luck yet.
-Is there any way to control the speed of the pumps i should look or time dripping seems better?
-The addition of RTC will be a good idea, as it can store real time measurements for later analysis, is that a complicated action for the arduino an holds enough space or it is easier for the RasPi to get and store readings?

Here's the sketch im using as prototype with 1 pump, 1 DHT11, 1 YL38+YL69

Please take your time and advice!

#include <dht.h>
#define DHT11_PIN 7
dht DHT;

int userStart = 0;

//define the pump 
int motorPin = A0; // pin that turns on the motor
int blinkPin = 13; // pin that turns on the LED
int watertime = 5; // how long to water in seconds
int waittime = 60; // how long to wait between watering, in minutes

// Define our pins.
const int soil = A1;
const int power = 12;
int output_value;

void setup() {
  pinMode(soil, INPUT);
  pinMode(power, OUTPUT);

  // Start with sensor OFF
  digitalWrite(power, LOW);
  
  pinMode(motorPin, OUTPUT); // set A0 to an output so we can use it to turn on the transistor
  pinMode(blinkPin, OUTPUT); 
  digitalWrite(motorPin, LOW);
  digitalWrite(blinkPin, LOW);

  Serial.begin(9600);
}


void loop() {
  if (Serial.available() > 0)  {
    switch(Serial.read())
        {
        case '1':
            // Turn sensor ON and wait a moment.
      digitalWrite(power, HIGH);
      Serial.print("Reading the Sensors ...");
      Serial.print('\n');
      delay(2000);
      output_value = analogRead(soil);
      output_value = map(output_value,1003,141,0.00,100.00);
      Serial.print("Food ");
      Serial.print(output_value);
      Serial.println("%");
    // Turn sensor OFF again.
    digitalWrite(power, LOW);
            break;
        case '2':
            Serial.flush();
  Serial.print("pump is ON");
  digitalWrite(motorPin, HIGH); // turn on the motor
  digitalWrite(blinkPin, HIGH); // turn on the LED
  delay(watertime*1000);        // multiply by 1000 to translate seconds to milliseconds
  digitalWrite(motorPin, LOW);  // turn off the motor
  digitalWrite(blinkPin, LOW);  // turn off the LED 
  Serial.print('\n');
  Serial.print("pump is OFF");}
 }

 //RH & temp
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Room Temp. c");
  Serial.println(DHT.temperature);
  Serial.print("RH %");
  Serial.println(DHT.humidity);
  delay(2000); 
            
}

I just found out a documentation for my relay, saying nothing more from what i already knew, means that 5V pins on the relay are used as OUTPUT, that stays without connecting anything on it. The numbered pins, are going to be my I/O pins of Arduino to the relay and GND gonna be the ground on my relay, is that right?

Relay documentation:

Moved as requested.

Hobb7t:
I just found out a documentation for my relay, saying nothing more from what i already knew...

The very first line of that document is a big fat lie.
These 16-relay boards do have opto couplers, but they are just ornamental.
No opto isolation with these boards. Only the 8-channel version has this option.
You can find the schematic diagram on the Sainsmart website.

The next incorrect information on that document is power draw.
The relay coils draw 75-80mA each, and the input/drive pins need a sink current of 2mA.
The 20mA stated seems to be pulled out of a hat.

There are two versions of this board.
The one without 12>5volt buck converter is shown (power parts missing).
That means this board needs a 5volt/1.25A (minimum) supply.
Leo..

This one (image attached), which im holding seems kinda the same with this one on the documentation. All this information means what?

Hobb7t:
This one (image attached), which im holding seems kinda the same with this one on the documentation. All this information means what?

That you will need a power supply with enough output to drive all 16 relays.

Buy a 5V 1.5A power supply to be on the safe side.

.

Hobb7t:
This one (image attached), which im holding seems kinda the same with this one on the documentation.

All this information means what?

It is the same.

It means that you can't rely on information from advertising.
To use this module, YOU have to know what you're doing regarding powering and driving.
It seems you didn't appreciate the correct information I gave you.
Leo..

Wawa:
It seems you didn't appreciate the correct information I gave you.

Sorry i din't meant to, i just don't understand yet some things. Im missing some serious info about opto-couplers.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Can you make a list of all your INPUT and OUTPUT devices, that is sensors and pumps and switches and indicator lights please.

This is a good place to start, we are glad you have some code working to prove you concept.

HINTS;

  • keep putting Pin in your pin number assignments, ie motor1Pin, motor2Pin, soil1Pin, powerPin etc, I noticed a couple missing the Pin annotation.
  • any value try soil1Val, soil2Val.

I am sure others will have more naming suggestions, this helps with reading and structuring you code.

With 16 "channels", it may be worth using arrays to define your variables, which will shorten your code.

If you are using a UNO then you will need to look at input and output multiplexing as the UNO will not have enough I/O pins, especially analog inputs.

I'm not sure what soil moisture sensors you are using, if it is the soil conductivity type then check for corrosion during use, this can effect the readings above the actual soil moisture.

Tom... :slight_smile:

Hobb7t:
Im missing some serious info about opto-couplers.

An opto coupler is a LED with a photo transistor in one package.
The transistor switches on/off depending on the light output of the LED.
There is no electrical connection between the two, just light.

They are used in circuits where you don't want an electrical connection between two separate circuits.
For safety, or to break ground loops, or to isolate 'dirty' circuits (motors) from 'cleaner' circuits (Arduino).

In your case, you might or might not need them.
Post a full diagram.
Leo..

TomGeorge:
Can you make a list of all your INPUT and OUTPUT devices, that is sensors and pumps and switches and indicator lights please.

With 16 "channels", it may be worth using arrays to define your variables, which will shorten your code.

If you are using a UNO then you will need to look at input and output multiplexing as the UNO will not have enough I/O pins, especially analog inputs.

I'm not sure what soil moisture sensors you are using, if it is the soil conductivity type then check for corrosion during use, this can effect the readings above the actual soil moisture.

Tom... :slight_smile:

Thanks Tom, i appreciate your advice, i need to keep them clear listed, i didn't had the time while i was trying to find ways or schemes on how to make it work. Soon ill update the code which right now works like a charm with the relay module.

At first i was really suspicious about the opto-couplers that Leo wrote. I was looking on my module, thinking if im gonna plug it and start the troubleshooting or if my project will be paused for a day at least by destroying my equipment.

Im using a DHT11 RH/Temp sensor, it is using 5v from the 5v pin of Arduino Uno, a digital pin as INPUT which is using the library of the specific module to map the readings. A soil humidity sensor (YL38(module)+YL69(probe)) which im using an analog INPUT, so we can have readings, a digital OUTPUT to turn on/off the module for some seconds(try to slow down the corrosion) and of course the soil hum. sen. is grounded on the Arduino. All the other digital pins are going to used as OUTPUTs to trigger actions on relays. Hopefully by multiplexing i can make it work, i still have some doubts, can i get clear readings through these 16 analogue channels like i did through the no-mulplex analogue pins on the Arduino? This device is like a GODMOD and i would like to read more.

Well here's what i found, while searching on google and because i have seen two different versions of this relay module one with parts missing, i though that is a good idea to search by looking the images on the web.

I found this picture on a simple guide which helps to connect high current devices to a low lever trigger module, the same that i own. So i could also connect high current devices, which will controlled by the relay.

At first i found out that i wired nearly perfect everything.

here's the plan

I was using a 12v 500ma plug directly to the Arduino, that was to help me run the pump when i was started the project (now there are 13 pumps). I was using a transistor to turn on and off the pump, running the 12v pump from the Vin pin of Arduino, using a diode between the legs of the pumps to prevent spikes return to the micro controller. So now i wired the Vin on the COM of all the relays, but also on VCC of the relay module (i don't know what i was thinking, but nothing bad happened so :wink: ), near the VCC was a GND which i wired to the Arduino, the "always open" side of relay connected to the + side of pump. The - to the GND on Arduino.

As you can imagine, when i was running the Arduino with a USB, everything seemed cool, low voltages on pumps, that nearly made them move, but relays were work like a charm. Then i decided to plug the 12v power supply, a move that turned on everything, making all the pumps and relays go on, like there's no code. Of course i rewired and used a jumper cable to get 5v from the Arduino and connect it to the VCC of the relay module. Now im just focusing on getting ASAP the Mux Shield and the soil hum. sen. but im also curious on how many relays can open instantly the same exact time (by coding and power consumption p.o.v.) and how many pumps can work together without any problem.

Wawa:
In your case, you might or might not need them.
Post a full diagram.
Leo..

Thanks for the information Leo, really useful
I will post diagram ASAP

Holly molly, i'm really lost with the Mux shield II addressing, like i can define rows, but how to define pins?