Hy everyone, i am struggling with a 16x2 lcd that keeps showing weird characters on the screen and i wasn't able to figure out the root of the problem.
Weird characters;
The setup is meant to control a cheap soldering iron;
- using an SSR fotek relay to turn it on/off when it reaches desired temperature
- arduino nano as the mcu
- potentiometer to set the desired temperature
- 16x2 LCD screen
- a green led that is wired to the relay input to show if it gets signal
- an input wire that goes through the relay to a socket for the soldering iron
- an 100k potentiometer from an ender 3 pro, placed inside the tip of the iron where a have drilled a hole, it's wires goes from inside to the mains plug, tied with tape to the 230v cable
- a 5.5v 3A socket psu that power the whole setup, arduino directly on 5v
- a switch for mains power cable that goes to the socket
At first i was using a wall 5v phone charger to power the setup wich was placed inside the enclosure with all the other components, my thoughts were that there is some sort of EMI that corrupts the data sent to the screen, caused by electronics inside the SSR, mains voltage wires that goes to the socket wich pass near the 5v arduino wires, noise caused by the transformer of the psu, and the screen placed right on top of these.
I have changed the psu with the one mentioned before, that sits outside the enclosure and delivers power through a round plug from wich there are some short wires that goes to the arduino 5v pin, potentiometer, screen..
The problem still persists but it doesn't happen right away, only after a few seconds/minutes when it starts showing weird characters and even end up being blank.
The solders of the screen and the i2C adapter are all good, i have added a ~47uF cap on the wires that power all of the things mentioned above. Another thing is that when i have chosen the resistor for the thermistor voltage divider, with a smaller one i got more problems with the screen than with a bigger one 6.6kohm that is now on the circuit.
I have thought that there is to much consumption from the Arduino nano and it can't handle all the components some times the green led shows that the setup works fine even it the screen doesn't but sometimes it all freezes and adjusting the potentiometer does nothing, the led + relay remaining on and the screen messed up.
Lately i have tried to power through the usb port on the arduino from a laptop that delivers about 4.5V and i didn't got much problems so far, the screen is not that bright. So why would powering to the 5v of the Arduino cause problems with the screen?
Here is the code;
#include <LiquidCrystal_I2C.h>
#include <math.h>
//A4(22) SDA
//A5(21) SCL
//A6(20) POTENTIOMETER
//D13 RELAY
//A0 THERMISTOR
int ThermistorPin = A0, releu = 13, pot = 20;
int inputTemp;
boolean bol = true;
int Vo, Tc;
float R1 = 6660; //6.66 kohm
float logR2, R2, T;
float c1 = 1.053121362e-3, c2 = 2.019510677e-4, c3 = 0.05061621614e-7;
LiquidCrystal_I2C lcd(0x27,16,2);
float floatMap(int x, float in_min, float in_max, float out_min, float out_max) {
return ((float)x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void temp(int set, int act){
R2 = R1 * (1023.0 / (float)act - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tc = round(T - 273.15);
// Tf = (Tc * 9.0)/ 5.0 + 32.0;
if(Tc>0){
lcd.setCursor(0,0);
lcd.print("Set temp = ");
lcd.print(set);
lcd.print(" C");
//Serial.println("Set temp = ");
//Serial.print(maped);
//Serial.println(" C");
lcd.setCursor(0,1);
lcd.print("Act temp = ");
lcd.print(Tc);
lcd.print(" C");
//Serial.println("Act temp = ");
//Serial.print(Tc);
//Serial.println(" C");
}
}
void action(int set){
if(Tc<0){
digitalWrite(releu, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Err termistor!");
delay(1000);
}else{
if((Tc-1)>set){
digitalWrite(releu,LOW);
//Serial.println("LOW");
} else if(Tc<set-4){
digitalWrite(releu,HIGH);
//Serial.println("HIGH");
}
}
}
void smooth(int& set, int& act){
float Setvalue = 0.0;
float Actvalue = 0.0;
float numReadings = 30.0;
for (int i = 0; i < int(numReadings); i++){
// Read light sensor data.
Setvalue = Setvalue + analogRead(pot);
Actvalue = Actvalue + analogRead(ThermistorPin);
// 1ms pause adds more stability between reads.
delay(1);
}
// Take an average of all the readings.
Setvalue = Setvalue / numReadings;
Actvalue = Actvalue / numReadings;
set = round(Setvalue);
act = round(Actvalue);
}
void setup() {
pinMode(ThermistorPin, INPUT);
pinMode(releu, OUTPUT);
digitalWrite(releu, LOW);
pinMode(pot, INPUT);
lcd.begin();
lcd.backlight(); // Make sure backlight is on
Serial.begin(9600);
}
void loop() {
smooth(inputTemp, Vo);
float maped = floatMap(inputTemp, 0.0, 1023.0, 300.0, 0.0);
int setTemperature = round(maped);
temp(setTemperature, Vo);
action(setTemperature);
delay(1000);
}
Code explanation;
I had problems with the analog readings from the potentiometer and the thermistor fluctuating with a few degrees so i made a smooth function to get the average over a couple of reading then mapped the value from 0-1023 to 0-300 and rounded the result. Now i get a very constant reading.
Then i have a function to calculate the soldering iron temperature based on the thermistor reading, using Steinhart's ecuation for wich i have used an online calculator to get the PID values by measuring the resistance of the thermistor at different temperatures.
It has being said that i have to use a resistor for the voltage divider about the same resistance as the thermistor at the temperature i will be using it. This case around 200 C and i got a pretty low value wich a don't remember, betwenn 100-2000 ohm i believe but when i tried using a resistor that low, the screen got worse so i ended up using a 6.66 kohm one.
The last function turn the relay on/off if the read temp is lower/higher within the specified amount, from the set temperature.
Here is a drawn schematic;
Photos;
I would appreciate any help!
Regards, Alex!