Arduino gets stuck after a while

Im driving 8 relay module using Arduino and at the same time using an 1602 LCD Display and a Ultra sound Scanner to detect if object exists.

Program below.

It works for a while, after a while it gets stuck. Even i cannot provide any characters using Serial Monitor/Plotter once stuck. No pattern on when it gets stuck

Im using Python in RPI to drive the Arduino.

No separate power supply used, however, Relay has its own 12v power supply.

Any help would be much appreciated.

#include <lcd_i2c.h>

char receivedChar;
boolean newData = false;

const int pingPin = 10; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 11; // Echo Pin of Ultrasonic Sensor

boolean nameDisplayFlag=false;
boolean pumpDisplayFlag=false;
boolean errorDisplayFlag=false;

long duration,cm;

char pumpDisplay[17] = “Dispense00000000”;

lcd_i2c lcd(0x3E,16,2);

void setup() {

Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

//US Scanner
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
// LCD
lcd.begin();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome ");

}

void loop() {

//Serial.println(cm);
recvInfo();
operateMotor();
if (checkNotDispensing() && nameDisplayFlag == false && errorDisplayFlag==false){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome … ");

  nameDisplayFlag = true;
  pumpDisplayFlag = false;
}

if (!checkNotDispensing() && pumpDisplayFlag == false && errorDisplayFlag==false){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Welcome…”);
lcd.setCursor(0,1);
lcd.print(“Dispensing…”);
nameDisplayFlag = false;
pumpDisplayFlag = true;
}

}

void recvInfo() {

if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);

if(cm > 15 && checkONData()){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome ");
lcd.setCursor(0,1);
receivedChar=‘Z’;
newData = false;
errorDisplayFlag=true;
lcd.print(“ABORTED-No Object”);
Serial.println(cm);
while(Serial.available() > 0) {Serial.read();}
}else{
errorDisplayFlag= false;
}//Serial.println(“V=”);

}

}

void operateMotor() {
while(newData == true) {

if( receivedChar == 'A'){
   digitalWrite(2, HIGH);    
   pumpDisplay[8]='0';
        
    
}
if(receivedChar == 'B'){
    
    pumpDisplay[8]='1';
    digitalWrite(2, LOW);    
  }
if( receivedChar == 'C'){
   digitalWrite(3, HIGH);
   pumpDisplay[9]='0';    
   
}
if(receivedChar == 'D'){
    pumpDisplay[9]='1';    
    digitalWrite(3, LOW);    
  }

  if( receivedChar == 'E'){
   digitalWrite(4, HIGH);    
    pumpDisplay[10]='0';    
        
}
if(receivedChar == 'F'){
  digitalWrite(4, LOW);    
  pumpDisplay[10]='1';    
  }
  if( receivedChar == 'G'){
   digitalWrite(5, HIGH);    
    pumpDisplay[11]='0';    
       
}
if(receivedChar == 'H'){
  pumpDisplay[11]='1';    
    digitalWrite(5, LOW);    
  }
  if( receivedChar == 'I'){
    pumpDisplay[12]='0';    
   digitalWrite(6, HIGH);    
}
if(receivedChar == 'J'){
    pumpDisplay[12]='1';    
    digitalWrite(6, LOW);    
  }
  if( receivedChar == 'K'){
    pumpDisplay[13]='0';    
   digitalWrite(7, HIGH);    
  }
if(receivedChar == 'L'){
    pumpDisplay[13]='1';    
    digitalWrite(7, LOW);    
  }
  if( receivedChar == 'M'){
   pumpDisplay[14]='0';    
   digitalWrite(8, HIGH);    
       
}
if(receivedChar == 'N'){
   pumpDisplay[14]='1';    
    digitalWrite(8, LOW);    
  }
  if( receivedChar == 'O'){
   pumpDisplay[15]='0';    
   digitalWrite(9, HIGH);    
     
}
if(receivedChar == 'P'){
  pumpDisplay[15]='1';    
    digitalWrite(9, LOW);    
  }

Serial.print(receivedChar);
newData = false;

}

}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
boolean checkNotDispensing(){
if( pumpDisplay[8] == ‘0’ && pumpDisplay[9] == ‘0’ && pumpDisplay[10] == ‘0’ && pumpDisplay[11] == ‘0’
&& pumpDisplay[12] == ‘0’ && pumpDisplay[13] == ‘0’ && pumpDisplay[14] == ‘0’
&& pumpDisplay[15] == ‘0’ ){
return true;
}
else{
return false;
}
}
boolean checkONData(){
if(receivedChar == ‘B’ || receivedChar == ‘D’|| receivedChar ==‘F’|| receivedChar ==‘H’|| receivedChar ==‘J’|| receivedChar ==‘L’|| receivedChar ==‘N’|| receivedChar ==‘P’){
return true;
} else{
return false;
}
}

is the arduino power isolated from the relays and what the relays drive?

Relay is powered with a separate 12v power supply and arduino is just used to drive the switching

@gcjr

This is probably EMI related to the relays or their loads - you need to provide a schematic for how the relays, loads and power supplies are connected.

Many of these relay modules can be connected either opto-isolated or not opto-isolated
and its not intuitive how to do this.

For opto-isolated use the only connections between Arduino and relay module should be
5V and the signal pins. The grounds are not shared, and furthermore there is a jumper
that needs to be set correctly to isolate the Arduino 5V from the relay driver 5V.

But it does depend on the particular specific relay module in question, so we need full
details of that unit too (generally full details of any hardware is useful to have, meaning
datasheets, product pages, whatever you’ve got.

no need for an opto-isolator. an open-collector circuit with common ground can drive the relay, but a back biased diode prevents voltage spikes

a uln2803a accepts a digital input and includes the diode

Im using a 5V 12V 8 Channel Relay Module Shield With Optocoupler.(JQC-3FF-S-Z 12V DC)

GND and JD-VCC of the relay is connected to the 12v power supply and VCC is connected to the 5v of the arduino. I have removed the jumper hence. The other connections from Arduino is the signal ones to Relay (pins 2-9)

Data sheet for the LCD [Display]: https://drive.google.com/file/d/1oKD2RR_impLviQ7R5FnXotGfLS-1VgDk/view

USS Scanner Data Sheet here: HC-SR04 User's_Manual - Google Docs

@gcjr @MarkT

The library for the LCD is not the LiquidCrystal one that comes default

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.