Problem with relay in project

Hello,

Karting is my hobby. For organizing kartraces people asked me to make a pit timer systeem. I thought it would be a nice and manageble Arduino project. I have selected the different components and asked someone from Fiver to program the code (im not good in that). But now i'm stuck and cannot finish it. Can somebody help me please?

I have added a presentation (see attachment) in which i explain the problem i'm facing. It describes what is happening now (with the code, connections and components) and what should happen. The code can be found below.

Thank you in advance!

Greetings Frits

The code is:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

unsigned long rpm_time = millis();
int timer = 5;  //set to 5sec, 
unsigned long timer_var = 0;
unsigned long timer_demo = 0;
unsigned long printing = 0;
//unsigned long update_time = millis();
//unsigned long calibration = 4000; 
//int x  = 0;
int kart_check = 0;
int flg = 0;
const int Rgreen = 7;
const int Rred = 6;
const int inc = 3;
const int dec = 4;


// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;  // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
int check = 0;


// Variables will change:
int ledState1 = HIGH;         // the current state of the output pin
int buttonState1;             // the current reading from the input pin
int lastButtonState1 = HIGH;  // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime1 = 0;  // the last time the output pin was toggled
unsigned long debounceDelay1 = 50;    // the debounce time; increase if the output flickers

int check1 = 0;


void setup() 
{
 Serial.begin(9600);
 Serial.println("      Kart Simple Timer Check");
 attachInterrupt(0, RPMCount, RISING); 
 pinMode(Rgreen, OUTPUT);
 pinMode(Rred, OUTPUT);
 digitalWrite(Rgreen,LOW); //green light ON in starting
 digitalWrite(Rred,HIGH); //red light OFF in starting
 pinMode(inc, INPUT_PULLUP); // enabling internal pull-up resistor
 pinMode(dec, INPUT_PULLUP); // enabling internal pull-up resistor
 lcd.begin();
 lcd.backlight();
 lcd.setCursor(0,0);
 lcd.print("Kart System");
}

void loop() 
{


debounce();
debounce1();

 if(millis()>= printing + 1000){
   printing = millis();
   Serial.print("Default Timer(sec):  ");
   Serial.println(timer); 
   lcd.setCursor(0,0);
   lcd.print("Timer: ");
   lcd.print(timer);
   }

 
 if(millis() <= (timer_var)){
 
    digitalWrite(Rred,LOW); //red light ON
    digitalWrite(Rgreen,HIGH); //green light OFF

     if(millis() >= printing + 1000){
       printing = millis();
       Serial.println("Red Signal ON ");
       Serial.print("Time Left(sec):  ");
       Serial.println((timer_var - millis())/1000);        
     }
 }
 else{
 
   digitalWrite(Rgreen,LOW); //green light ON 
   digitalWrite(Rred,HIGH); //red light OFF

   if(millis() >= printing + 1500){
       printing = millis();
       Serial.println("Green Signal ON ");
     }

     //x = 0;
     kart_check = 0;
     flg = 0;
     Serial.println("Flag Reset");

     
 }
 
 //  if(millis() >= (calibration + update_time)){
   //   update_time = millis();
        
     if((kart_check == 1) && (flg == 0)){
          Serial.println("New Kart Entry ");
      //  delay(3000);  //addition of 3sec delay
        rpm_time = millis();
        timer_var = rpm_time + (timer*1000) ;  
     //   if (timer_demo > rpm_time ){
       //     timer_var = (((rpm_time + (timer*1000)) - (timer_demo - rpm_time)) + (rpm_time + (timer*1000)));  //((set time - time left)+ set time) ) ;    
       // }
        timer_demo = timer_var;
        Serial.print("Total Time(sec):  ");
        Serial.println((timer_var - rpm_time)/1000);
        //x = 0;
        flg = 1;
     }
   //} 
}

void RPMCount()                        
{
 Serial.println("Y");
 // x = 1;  
  kart_check = 1 ;                   
}



void debounce(){
   // read the state of the switch into a local variable:
 int reading = digitalRead(inc);

 // check to see if you just pressed the button
 // (i.e. the input went from LOW to HIGH), and you've waited long enough
 // since the last press to ignore any noise:

 // If the switch changed, due to noise or pressing:
 if (reading != lastButtonState) {
   // reset the debouncing timer
   lastDebounceTime = millis();
 }

 if ((millis() - lastDebounceTime) > debounceDelay) {
   // whatever the reading is at, it's been there for longer than the debounce
   // delay, so take it as the actual current state:

   // if the button state has changed:
   if (reading != buttonState) {
     buttonState = reading;
     // only toggle the LED if the new button state is HIGH
     if (buttonState == HIGH) {
        check = !check;
        timer =  timer + 1;
        Serial.print("New Timer(sec):  ");
        Serial.println(timer);
        lcd.setCursor(0,0);
        lcd.print("New Timer: ");
        lcd.print(timer);
        ledState = !ledState;
       
     }
   }
 }

//Serial.print("check: ");  Serial.println(check);


 // save the reading. Next time through the loop, it'll be the lastButtonState:
 lastButtonState = reading;
 }



void debounce1(){
   // read the state of the switch into a local variable:
 int reading1 = digitalRead(dec);

 // check to see if you just pressed the button
 // (i.e. the input went from LOW to HIGH), and you've waited long enough
 // since the last press to ignore any noise:

 // If the switch changed, due to noise or pressing:
 if (reading1 != lastButtonState1) {
   // reset the debouncing timer
   lastDebounceTime1 = millis();
 }

 if ((millis() - lastDebounceTime1) > debounceDelay1) {
   // whatever the reading is at, it's been there for longer than the debounce
   // delay, so take it as the actual current state:

   // if the button state has changed:
   if (reading1 != buttonState1) {
     buttonState1 = reading1;
     // only toggle the LED if the new button state is HIGH
     if (buttonState1 == HIGH) {
        check1 = !check1;
       ledState1 = !ledState1;
       timer =  timer - 1;
       Serial.print("New Timer(sec):  ");
       Serial.println(timer);
       lcd.setCursor(0,0);
       lcd.print("New Timer: ");
       lcd.print(timer);
     }
   }
 }

Serial.print("check1: ");  Serial.println(check1);


 // save the reading. Next time through the loop, it'll be the lastButtonState:
 lastButtonState1 = reading1;
 }

Timer pits 2019.pdf (1.52 MB)

Pittimer.ino (6.56 KB)

Please read the first topic, how to use Forum and, #7, how to attach code. Many helpers use smart phones that has no IDE so they can't read Your code, and You get less help.

Hi Railroader,

Thank you for your help. I have posted the code in the message now, and i will try to use the forum as meant to be.

Thanks for the code but the instructions given tells You to use code tags, </>, surrounding the code, to the very left of the function line. That creates a window and within that the code will scroll.
Second. What is happening and what would You like to happend? Observing what happends is worth a lot for any investigation. Reading "dry code" is a much, much harder, and more time consuming way to find out.
Describe what works and what doesn't.

Now I have put in the code in the right way in the message. I also described more clearly that the underlying information (what is happing now, and what should happen) can be found in the attachment. I hope its more clear now :slight_smile:

Hi,
Your pdf has a circuit image, but the circled areas do not make sense,


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Reverse engineer your project to make your schematic, do not just copy the Fritzy image.

What are you using for your power supply?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Your pdf has a circuit image, but the circled areas do not make sense,


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Reverse engineer your project to make your schematic, do not just copy the Fritzy image.

What are you using for your power supply?

Thanks.. Tom... :slight_smile:

Hi Tom,
I have drawn the circuit by hand. It's some kind of hybrid schema since i don't know how to properly draw a scheme. I hope you can understand it. :slight_smile:
I have also added a picture of the adapters i'm using. I think they are some kind of phone chargers. I have tried different chargers, but with no effect. In the attachment you can find the scheme and the picture.

Thank you very much, Greetings, Frits

Hi,
Thanks for the images;


Tom.. :slight_smile:

Hi Tom,

Your question about the power adapters gave me some new inspiration to find the problem. I found a detailed link with how to properly feeding power to an Arduino: Link

I found some points to improve the power supply, which i will test tonight. I will report the results :slight_smile:

I have just tested different ways to power the Arduino. I tried the USB port of the Arduino connecting to an computer and also with a powerbank. I also did this for the relay itself. After that i tried the barrelconnector in combination with an adjustable power supply (Link) and with different voltages ( 7 - 12 V). All of this did not help :confused:

I see you are using relay modules and other things.

Do not (ever!) power the UNO via the "barrel jack" or "Vin" pin because these use the on-board regulator which is not capable of effectively powering any external devices of note.

You need a 5 V power supply. Generally, a "Phone charger" with a USB output connector such as you have will be most convenient and practical. The USB port is limited to 500 mA, so if something is going to use more than that device you need to connect the 5 V power supply to that device and also back to the Arduino using the "5V" pin.

And yes, your "Fritzing" diagram is useless. If you were using a breadboard in the fashion depicted, you have not actually connected anything! :roll_eyes:

"Do not (ever!) power the UNO via the "barrel jack" or "Vin" pin because these use the on-board regulator which is not capable of effectively powering any external devices of note."

I think a better way to explain this is that the on-board voltage regulator that is connected to the barrel jack does not normally have enough current capacity at the 5V pin to drive much in the way of power-hungry peripherals, like the relay module. The relay module should be powered separately.

Hi Paul and Steve,

Thank you for the advise. So from now on i will use the 5V pin on the Arduino to power it.
I will do some more testing!

A user of the forum advised me to do test with the IR sensor. F.E. use a normal push button. When i did this, the pittimer was working fine, also under load!
Now i will make some changes to the code, and see if that is working!

Keep you informed,

Frits

Do not (ever!) power the UNO via the "barrel jack" or "Vin" pin because these use the on-board regulator which is not capable of effectively powering any external devices of note."

I think a better way to explain this is that the on-board voltage regulator that is connected to the barrel jack does not normally have enough current capacity at the 5V pin to drive much in the way of power-hungry peripherals, like the relay module.

You can power the board through the barrel jack but must limit current in or out of the pins to 200mA or less and should test how hot the Uno gets. At least up to Rev 3 the Arduino barrel jack and VIN power go through a linear regulator that wastes voltage > 5V into heat so -- 7V to VIN will run cooler than 12V.

Even if you use a DC-DC buck converter to turn > 5V into 5V (efficiently and only the converter gets any warm) and power the board into the 5V pin hole, do not run > 200mA through the board and it is best to keep the total to 100mA or less.

If you feed the relay from the same power supply as the board then make sure that the supply is good for twice or more what the relay needs. Otherwise when the relay draws power the current going into the Uno may drop, causing the voltage to drop and the Uno may reset. You -can- feed the Uno 5V and feed the relay 9V or more from a separate supply.

GoForSmoke:
Even if you use a DC-DC buck converter to turn > 5V into 5V (efficiently and only the converter gets any warm) and power the board into the 5V pin hole, do not run > 200mA through the board and it is best to keep the total to 100mA or less.

That limitation relates to the total port current rating of the ATmega328.

If you are using the "5V" pin as a power input - which is indeed, the best approach - there really is no other 5 V "output" except for 20 mA or so at a time from I/O pins. Or perhaps a few mA from the 3.3 V pin.

You have a (regulated) 5 V supply. You feed it to the "5V" pin and also - in parallel - to whatever other assemblies require 5 V.

Note if you are using a 5 V relay module from the same power supply, then the 5 V and ground power wires to that relay module run directly (and together as a pair) from the output terminals of the power supply - the point at which the output bypass capacitor is placed - while the 5 V and ground to the Arduino also separately run together as a pair from those output terminals of the power supply.

This separates any current surges/ dips that the relay board generates, from the Arduino supply. (There really should be a 1 mF capacitor across 5 V and ground at the relay board itself.)

If the relay board has separate "Vcc" and "JD-VCC" terminals, the "JD-VCC" runs to the power supply, while the "VCC" runs together with the "IN" connections back to "5V" on the Arduino.

Hello,

With help from the forum i have identified the problem. When a load was connected to the relay, the sensor values of the IR-sonsor increased. This caused the problem with the relay switching unwantedly. I found out that the sensor is normally connected to an analog input, so i changed that, and also that i could set the value for the ir-values when it should switch. After this all works fine!

Thank you all for the help! I'm very happy :slight_smile:

Does the relay module have an indicator led for when it's ON?

Yes it does have an indicator led for showing its on or off.

Red leds put out a lot of IR I know for fact viewing them with CCD cams (even through IR filter the led shows as white not red) and using red leds to trigger IR-phototransistors (black bulb, 2 leads). There's a good chance that all leds put out near-IR and lower.

Cover the led. See if the problem goes away.

PS - using a red led to test IR detect makes it easier to know when the led is really ON, not just supposed to be ON.