I tried to make an LCD that show the temperature using LM35 sensor. When the temperature reach 40 degrees, the arduino should turn on the relay in pin 10. The code is like this :
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int sensor1 = A0;
int sensor2 = A1;
int suhuadc1;
int suhuadc2;
float tegangan1;
float tegangan2;
float nilaisuhu1;
float nilaisuhu2;
int suhuakhir2;
int suhuakhir1;
int relay1 = 10;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Dispenser");
lcd.clear();
pinMode(relay1, OUTPUT);
}
void bacasuhu() {
suhuadc1 = analogRead(sensor1);
suhuadc2 = analogRead(sensor2);
// converting that reading to voltage, for 3.3v arduino use 3.3
tegangan1 = suhuadc1 * 5000.0 / 1024;
tegangan2 = suhuadc2 * 5000.0 / 1024;
nilaisuhu1 = tegangan1/10;
nilaisuhu2 = tegangan2/10;
suhuakhir1 = (int) nilaisuhu1;
suhuakhir2 = (int) nilaisuhu2;
lcd.setCursor(0,0);
lcd.print("T1 :");
lcd.setCursor(5,0);
lcd.print(suhuakhir1);
lcd.setCursor(0,1);
lcd.print("T2 :");
lcd.setCursor(5,1);
lcd.print(suhuakhir2);
delay(1000);
}
void loop() {
bacasuhu();
if (suhuakhir1>=40) {
digitalWrite(relay1,HIGH);
}
else {
digitalWrite(relay1,LOW);
}
}
I don't know why, but after the temperature reach 40 degrees the temperature that read from the sensor become so unstable and inaccurate. So what's going on and what to do to fix that?
Probably either the way you have wired it up, or your power supply not being able to provide enough current. But that is just a guess as you have posted no schematic nor photograph (not exceeding 1000 pixels).
Thanks @Grumpy_Mike and @jremington for the response. I connected the 5v relays direct to the arduino without driver. It takes about 40-90mA current to move the coil. It is SRS songle relay.
Should I use the driver with a transistor to solve the problem?
You will eventually destroy the Arduino, or at least burn out the output pin, by connecting the relay as you have. Connect it using a circuit similar to that shown below (but use a smaller base resistor, say 470 ohms).
The Arduion output is only good for 20 to 30 mA. At a draw of 40mA damage starts to happen.
With your realy directly connected and no back EMF diode I assume, you are killing your processor so no wonder the A/D converter is playing up.
Use the relay driver like jremington said.
I don't know why, but it still error in reading Second sensor (A1). Is that because I used wrong transistor (2N3904)? is 2N3904 has too small current? what is the best transistor to drive 5v relay?
This fault can be caused by ground lift if the sensor and the relay are wired so that there is a section of wiring that carries the ground for both the relay and the sensor. I want to know if you have made that error.
The solution is to use star wiring of both power and ground. That is have the power and ground for relay board and sensor wired to physically the same point, not chained.
With star wiring the grounds must physically meet at one point and separate as much as possible the analogue from the digital side. On the Arduino there are three ground points so use one for the analogue ground returns and the other for the digital ground returns. Have the power wires physically wired to the same spot, NOT chained one to the other. This is why I am asking for a photograph of your wiring. Why will you not co-operate with me?
Here is a better way of wiring up what you have but as you insist on not providing a FULL circuit of what you have it is hard to be definitive.