3 Sensors, 2x Relays 2x LEDS

Hi Guys,

First post so I’ll say a quick howdy, name’s Sam from Aus, always been a tinkerer/hacker of things and am needing some if/else for my projects. So a few questions from the new guy (have watched a lot of video tutorials and done all the basic i/o tests with the board but have some specific questions I am wondering if someone could help me out with.

I have Moisture, Light and Temp sensors to control a couple of relays I have ordered (http://australianrobotics.com.au/products/twig-smart-relay) and also a couple of LEDS for error messages.

and if anyone guessed what it is…Its just another garduino project for now, however the idea is to build some custom PLC boxes for local farmers to use, coupled with a long range xbee they can control pumps and other orchard equiptment, etc.

I have attached the Fritzing Sketch below but my questions three be.

  1. Can I run 2 relays, 3 sensors and 2 LEDS of an arduino 5v rail?
  2. If not then I’ll use the second board in my sketch with a proper layout for the secondary power and 5v regulator + decoupling caps) So, will I need to bridge ground between the secondary relay board power and the arduino power?
  3. I purchased a LM335 (Z) Analog Temp. Sensor and for the life of me every wiring diagram I have found to hook this thing up to arduino with many different valued resistors my output is usually around -200C or 426Kelvin or some other really random number. Does anyone have a diagram and code they have seen online that they know works?

Again if you take the time to read, and or reply.

Many thanks.
Sam

Garduino.fz (391 KB)

  1. Can I run 2 relays, 3 sensors and 2 LEDS of an arduino 5v rail?

Maybe. Maybe not. Depends on the relays and sensors. The LEDs, with proper current limiting resistors, should present no problems.

So, what kind of relays are they? Links, please?

Sensors?

Hi Pauls,

The link is following the relays text

control a couple of relays I have ordered (http://australianrobotics.com.au/products/twig-smart-relay)

1x Standard Photoresistor
1x Moisture sensor (2 galvanised nails, with one nail to 5V on Arduino and the other via a 10K resistor to ground and then to Analog0. Works a treat!
1x LM335 (Z) Analog Temp sensor. http://www.australianrobotics.com.au/products/lm335-z-precision-analog-temperature-sensor (This I can’t seem to get working)

The link you gave for that relay says it takes 40mA switching current. As the relay clearly has a transistor on the board, I’m guessing that it takes much less than that from the Arduino output pin, and takes 40mA from the 5v rail. That gives 80mA for the relays, plus the LEDs (usually 10 to 20 mA each, depending on the series resistor), plus the sensors. You can draw up to 500mA less the current consumption of the Arduino itself from the 5v rail, so you should be OK.

Regarding the temperature sensor, if you connect it via a series resistor of about 2Kohm to the 5V supply (observing correct polarity) and measure the voltage across it with a multimeter, what voltage do you get? Should be about 3v according to the data sheet.

Thank you heaps dc42, you are a gentleman and a saint!

I have gotten the temp sensor hooked up as asked but my multimeter isn't working for some reason.

I'm using the following code to get this:

116C - 240F (actual temp with a digital thermo is: 15C

int inPin = 0;                              // select the input pin for analog temp value
int inVal;                                  // integer value for input read from sensor
int delayVal = 1000;                        // delay value to read once a second

void setup() {
Serial.begin(9600);                         // set up Serial library at 9600 bps
}

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor
  inVal /= 2;                               // convert 1024 steps into value referenced to +5 volts
  Serial.print(inVal);                      // print input value
  Serial.print(" Celsius,  ");              // print Celsius label
  Serial.print((inVal * 9)/ 5 + 32);        // convert Celsius to Fahrenheit
  Serial.println(" Fahrenheit");            // print Fahrenheit label
  delay(delayVal);                          // delay the program for the requested time
}

So I think I am screwing my maths up somewhere here. I have it like:

  • /|---> Gnd
  • | |-----------Center to A0
  • |---> +5v through a 2.2K resistor

The only other thing is those two Twig Relays have four inputs, GND, VCC, NC, and SIG do I need to use all of these?

Thank you again.

  1. The centre pin of the temp sensor should be left disconnected. It's only used if you want to calibrate the sensor for greater accuracy, see the data sheet http://www.st.com/stonline/books/pdf/docs/2158.pdf. Connect A0 to the junction of the +ve pin and the resistor instead.

  2. "inval" that you are printing should be around 600 at room temperature. The calculation should be:

Temp (celcius) = ((Ain * 500)/1023) - 273.16

Bear in mind that Ain * 500 will overflow an int, so you need to do the calculation using either float or long int type, e.g.

int celcius = ((long)inval * 500L)/1023L - 273;

  1. You need to connect Vcc and Gnd of the relays to Arduino Vcc and Gnd, and Sig to the Arduino output pin that you want to control the relay. NC means no connection.

Hope this helps.

Hi dc42,

I seem to be rather thick today, trying to implement your code and am having difficulties. Trying to understand what is Ain (google doesn't help there)

However after A0 Junction was removed from center pin and placed after the resister on VCC I get .212 Celsius, 413 Fahrenheit

Wouldnt' be able to bug you for some mode code assistance would I?

ps: Relay chart was a dream!

By Ain I meant the reading you get from the analogRead call. So you could use something like this:

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  int celsius = ((long)inval * 500L)/1023L - 273;
  Serial.print(celsius; 
  Serial.print(" Celsius,  ");              // print Celsius label
  Serial.print((celsius * 9)/ 5 + 32);        // convert Celsius to Fahrenheit
  Serial.println(" Fahrenheit");            // print Fahrenheit label
  delay(delayVal);                          // delay the program for the requested time
}

A couple of other things:

  1. If you are having trouble, get your program to print the value read by analogRead directly. It should be around 600, corresponding to 3 volts across the sensor. If it is nowhere near that, then you have faulty wiring or a dead sensor.

  2. If you want to display fractional degrees, use floating point math:

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  int celsius = ((float)inval * 0.48876) - 273.16;
  Serial.print(celsius, 1); 
  Serial.print(" Celsius,  ");              // print Celsius label
  Serial.print((celsius * 9.0)/ 5.0 + 32.0, 1);        // convert Celsius to Fahrenheit
  Serial.println(" Fahrenheit");            // print Fahrenheit label
  delay(delayVal);                          // delay the program for the requested time
}

The resolution of the ADC corresponds to about 0.5 deg C so no point in displaying more than 1 decimal digit.

  1. The uncalibrated temperature error may be up to 6C at 25C. To calibrate it, either add a pot to the circuit as shown in the data sheet, or just change the value 0.48876 in the code above to the value that gives the right answer.

The code I tried mocking up was not too disimilar but I get a 'inval" was not declared in this scope on line "int celsius = ((float)inval * 0.48876) - 273.16;"

Full Code

int inPin = 2;                              // select the input pin for analog temp value
int inVal;                                  // integer value for input read from sensor
int delayVal = 2000;                        // delay value to read once a second


void setup() {
Serial.begin(9600);                         // set up Serial library at 9600 bps
}

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  int celsius = ((float)inval * 0.48876) - 273.16;
  Serial.print(celsius, 1); 
  Serial.print(" Celsius,  ");              // print Celsius label
  Serial.print((celsius * 9.0)/ 5.0 + 32.0, 1);        // convert Celsius to Fahrenheit
  Serial.println(" Fahrenheit");            // print Fahrenheit label
  delay(delayVal);                          // delay the program for the requested time
}

wouldn't be an issue if I could understand it fully, but at the moment this float inval stumps me, am I meant to change the part above the void loop() ?

sorry, should be inVal not inval.

also just realised that the temperature reading will vary strongly with Vcc voltage. if you are running from USB this will vary depending on what the computer is doing. you need either a well-regulated 5V supply (the on board regulator should be ok), or else you need to change the circuit and configure the adc to use the internal voltage reference instead of vcc.

So change the V and ran the code, but Serial Monitor shows nothing and the TX light blinks off the chart, checked Baud rate, slowed the delay but still nothing on the serial monitor.

Strange, all the print lines seem fine.

i spotted another mistake: "int celsius" should have been "float celsius". Not sure if this explains the problem though. if it still doesn't work try:

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  Serial.print(inVal); 
  delay(delayVal);                          // delay the program for the requested time
}

and see if it prints about 600.

Thanks! I'll try that code now, I did quickly run the second code

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  Serial.print(inVal); 
  delay(delayVal);                          // delay the program for the requested time
}

and am getting 424, my other thermometer says its 24Celcius

so I took your code and made it to

int inPin = 0;                              // select the input pin for analog temp value
int inVal;                                  // integer value for input read from sensor
int delayVal = 2000;                        // delay value to read once a second

void setup() {
Serial.begin(9600);                         // set up Serial library at 9600 bps
}

void loop() {
  inVal = analogRead(inPin);                // read the value from the sensor                     
  float celsius = ((float)inVal * 0.48876) - 183;
  Serial.print(celsius, 1); 
  Serial.print(" Celsius,  ");              // print Celsius label
  Serial.print((celsius * 9.0)/ 5.0 + 32.0, 1);        // convert Celsius to Fahrenheit
  Serial.println(" Fahrenheit");            // print Fahrenheit label
  delay(delayVal);                          // delay the program for the requested time
}

this gives me 24.2 Celsius, 75.6 Fahrenheit which matches it right. now I have turned off the heater and opened the door. and its not seeming to budge.

Wait....wait

it is! the colder the room gets the Hotter my arduino seems to think it is! its now 22 inside and arduino is 25.2

A reading of 424 is way out. Take a look at the data sheet at http://www.st.com/stonline/books/pdf/docs/2158.pdf. The V- pin is at one and, the V+ pin is the one in the middle, and ADJ is the other outside pin. This doesn't agree with the diagram in one of your earlier posts. You need to connect V- to GND, V+ to A0 and the 2k2 resistor, and the other end of the resistor to +5v. Leave ADJ unconnected.

Grr tried the picking up the A0 on either side of the resistor and I am sure I have it the right way around. however here is a pic (I know it’s a transistor but its the right shape and angled the right way)

Putting the A0 pin between the V+ and resistor also doesn’t work.

hmm.jpg

I did a design about 20 years ago… a temp sensor going into op-amp and translated to 0 - 5 V. It use a LM334. I check the datasheet for the part LM335. Here a schematic. I hope you ca read it. ( use zoom ) The schematic is from one my old unfinnish robotic project.

Samuel, your wiring diagram still shows the wrong wiring. If the flat side of the sensor is at the bottom in your diagram and the rounded side is on top (it's hard to tell from the picture because both top and bottom look flat), you need to do this:

  1. Connect the RIGHT lead of the sensor to GND instead of the left lead.

  2. Connect the MIDDLE lead of the sensor to the resistor, not the right lead.

  3. Connect A0 to the MIDDLE lead of the sensor, not to 5v.

  4. Leave the LEFT lead of the sensor not connected.

If the flat side of the sensor is at the top of the diagram, then swap left & right in the above - in this case you already have the left pin connected correctly but you need to move the left end of the resistor and the A0 connection to the middle lead of the sensor.

Please look at the data sheet to see for yourself which are the + and - leads of the sensor.

OK I finally downloaded fritzing so here is a diagram, with the sensor flat side at the top