Super new to arduino and 0 coding experience or forum experience.
I am trying to use a relay with 2 push buttons one for on and one for off i have a 12volt spdt inland relay. What i have now is when green is pushed it records the timestamp when was turned on and displays on. And when off is pushed it timestamps when it was turned off and displays off. The problem i have is both switches will switch the relay. I only want green one to turn on and red one to turn off the relay.
// set pin numbers:
const int button_ON = 2;
const int button_Off = 3; // the number of the pushbutton pin
const int Relay = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(Relay, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button_ON, INPUT);
pinMode(button_Off, INPUT);
digitalWrite(Relay, LOW);
}
void loop() {
if (digitalRead(button_ON) == HIGH) {
digitalWrite(Relay, HIGH);
Serial.println("Relay ON");
delay (250);
} else {
digitalWrite(Relay, LOW);
}
if (digitalRead(button_Off) == LOW) {
digitalWrite(Relay,LOW);
Serial.println("Relay OFF");
delay (250);
}
}
everything is doing and printing how it should in serial monitor but i cant seem to figure out hpw to get the on button to only do on and the off button to do only off.
Any help would be great.
that's fine, but unless you have a pull-up resistor, you'll need to use INPUT_PULLUP as the pin mode for those buttons, which should be wired between the input pin and ground, each, and both checked for being LOW to indicate they are pressed.
I can't from where I sit, but that's one big change, lose the else clause, and some tweaks to ake the buttons work and you can see that they should be treated the same. If a button reads LOW, it is pressed, when they are wired between the input pin and ground.
HTH and I look again when I am not moving.
And this one reasonis why the code you posted doesn't compile:
And… the delay() calls are unnecessary, except to reduce the frequency at which your code will keep reporting what a pressed button is doing, harmlessly turning on or off something that already is. On or off.
Also not compiling because - Case Counts, you have many Capital letters the shouldn't be. Capital. You can't possibly be describing the behaviour of the code you posted.
alto777 thanks for the response the code compiles fine i was trying to hand type it from my phone and it forced uppercase letters at the beginning i tried to find any other errors as i was away from a computer to copy and paste.
to start,
the delay is so i dont get so many readings when a button is pushed hence why i think i have the one set to low otherwise it just keeps printing on non stop. background in what i am trying to do i work in a manufacturing plant. and i would like to record actual up and down time of a machine. being the machine is turned on and off with push buttons my thought was to connect arduino to the push buttons and turn the relay on and off with those make the arduino in an in between just for the counter. that way i can drop the serial monitor numbers into and excel sheet and lets say count in 6 hours how long the machine was on and how long it was off. i am trying to record downtime on a machine. since the operators do not record and this way i have an exact amount of time. to see if its worth throwing some money at upgrades if the downtime ends up being a big number. looking for parts per hour of actual machine up time. being its a machine on needs to only be on and off needs to only be off. i will try to post a picture of how my buttons are setup here shortly need to figure out where to make a formal drawing first
Just put it all aside until you don't have the problems of working over the phone. When you can, pencil and paper are fine for diagrams and stuff, with things this simple anyway.
And trust me when I said everything I said, Imma guarantee you I am 98.6 percent sure of what I wrote on this matter here so far.
Let's first get your very simple sketch working with all the shortcomings of how it will need to be done for realz.
I assume your block of text was blamed on the phone also. It does take a bit of extra care, but you can use punctuation and capitalization and so forth, see how good I am doing?
railroader,
these are machines in manufacturing company. i am looking to record the timestamp when on was pushed an when off was pushed so i can record the time between button pushes in a certain time period to see how long the machine was up or down and calculate actual parts per hour. So they need a on and off button more of an estop basically.
alto777,
I am now at my computer. So I did what was suggested and deleted the else low line and copied code from my arduino app directly so it should be showing correct now i will post a new update for you now. sorry about my sentence writing I am not a very good writer when it comes to grammar and punctuation. It was never my strong suit. I will try to clean it up I am always learning something new. I have paper and pen now I am drawing it up and will post shortly.
but for now here is updated with that line removed.
// set pin numbers:
const int button_ON = 2;
const int button_Off = 3; // the number of the pushbutton pin
const int Relay = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(Relay, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button_ON, INPUT);
pinMode(button_Off, INPUT);
digitalWrite(Relay, LOW);
}
void loop() {
if (digitalRead(button_ON) == HIGH) {
digitalWrite(Relay, HIGH);
Serial.println("Relay ON");
delay (250);
}
if (digitalRead(button_Off) == LOW) {
digitalWrite(Relay,LOW);
Serial.println("Relay OFF");
delay (250);
}
}
Here is my shotty drawing no clue how to draw electrical diagrams. But I am Hoping this makes sense if not ill redraw. Upon further inspecting i suspect my wiring is a little funky. It seems i have a different relay than anything Ican find to topic search. It has 2 wires for trigger instead of just in1. Its an inland relay. Also remebering the else code that i deleted was there only to serial print once. I am now remebering I put a resistor on pin2 to ground so that it only serial print once. TO BE HONEST I suspect I may going about this the wrong way mostly because my lack of coding experience (none) so i dont know im going at this the wrong way
So this is what i have come up with it turns relay on and off with both switches and only prints to serial monitor one line every time i push each button. The serial monitor is doing exactly what i want it to do. Its the buttons that are doing to much. Upon exploring the option of just having it connected straight to the buttons and not using pin 13 at all. Button state change i think is the next thing that could get used. So if the last thing it writes is button on is there a way to check the button last state was on then dont do anything if on button is pushed again. Until it sees a button state chamge from other button Same with off if already off dont do anything with button push again ? Is this a way to do this?
Thank you!
Here's a much simpler way to do the same thing - made simple because you do have two pushbuttons. I don't know what @alto777 was going on about, really.
So either work off this, or learn about full-blown state change detection, or both.
Hey it works great with the light bulb setup! my relay however does not operate the same way. This is the issue i am running into. Maybe I have a odd relay. Its inland brand from Microcenter. So like i said light bulb works great serial monitor is writing exactly what it should but... heres the issue i ran into. When i push the on button it lights up the arduino and holds it on. But relay does nothing. However when i hit the off button thats when the relay led lights up. Then i hit the on button again it does nothing relay still is on its when i hit the off button again thats when the relay led shuts off its like its a weird trigger this is the problem i am trying to overcome. With a multimeter hooked up the relay has to see 0 volt and 5 volts again to switch i dont feel like this is normal. Why does it have to see 0 than 5 to switch. Than 0 and 5 again to switch. I deal with cars im unfamiliar with this type of relay i guess.
Photo of relay i tried learning how to read the datasheet. But without figuring out what i need to know about the data sheet i dont know where to look to find why it does what it does. Do i need what they call a bc548? Not sure what that is i am going to google it now. I learn little tid bits every deep dive search. Again thank you for the help i am learning as i go and trying to figure as much out off your guys information using google not afraid to search myself just need direction where to go. First arduino board 3 days ago thats how new I am. I am working with this first step once I nail it down I have a RTC that is next to put on. I also have the Feather M0 datalogger which has the RTC built in and sd card. the plan is to do that next the sd card is so I can write to that since it wont have a monitor than I will just pull the sd card for now. We dont have wifi in our building otherwise I would have went that route to send information. Doing stoneage things still.
Where the print statements are in each button code section, just write the relay HIGH, delay 100 milliseconds and write it LOW, cresting the pulse which will toggle the relay.
I didn't find the datasheet for the relay; a problem here is knowing the state of the relay because toggle just means switch.
IMO it wpuld be better to procure a normal relay module that energized when written one value LOW or HIGh, and is not energized when written the opposite value.
Some relays allow choosing, some are active HIGH and some active LOW.
Alto777 would you happen to have a suggested relay? Ive been looking this i think is only normal one i can find and its an 8 i could use just one now till i get a different one in the mail.
I found a forum with this relay they have somewhat of a data sheet. But the wires are for a movable push button and it is set to normally open. And they call it a latching relay needs 0 and 1 to switch it. One guy said get a normal one, one guy said set it to high delay quick first and one guy said drip a pin in the ground and other to the trigger wore and set it to 0 ,1 quick. But i think weve narrowed it down to what is going on. Not knowing what i needed to buy to start with.
When I googled "inland relay module" at Microcenter there was a single relay module that is not button controlled. I believe this is one that would work in a regular manner.
But there is nothing wrong with using the one you have and meeting it more than halfway by simple cresting the pulses where you need to to make it toggle.
Although I expressed concern about knowing the state, it is at least probable that it would power up into a known state, then your program logic could just keep its own track.
Technically this is open loop control, as you would be trusting the relay to text properly. all the relays and modules mentioned here or seen in the search at Microcenter are cheap for a reason… I would have no recommendation for a relay meant to control anything important, that's a judgement you'lol have to make, how important is consistent reliable switching?
Alright, thank you guys for the help. I am going to post the code that is working great for me its doing everything it needs to be doing hooked up to the machine the relay is Switching a 26volts dc. The link to wowki is great I am actually signed up now for it. it was great in seeing and testing there versus pricking away at the board. this relay is a latchkey relay needs to see 0 then minnimum 2.3 to switch. The comment from alto777 "Where the print statements are in each button code section, just write the relay HIGH, delay 100 milliseconds and write it LOW, cresting the pulse which will toggle the relay." this is what enabled that relay to work correctly. also i did find out switching over to the NO buttons not the arduino ones was a good learning experience in how the arduino are different in a small way in that they are still NO button but need to be used kiddie corner from eachother to act like arduino push button.
// https://wokwi.com/projects/388209760447386625
// https://forum.arduino.cc/t/12volt-relay-with-1-push-button-on-and-1-push-button-off/1217076
const int button_On = 2;
const int button_Off = 3;
const int Relay = 13;
int buttonState = 0;
void setup(){
Serial.begin(9600);
pinMode(Relay, OUTPUT);
pinMode(button_On, INPUT_PULLUP);
pinMode(button_Off, INPUT_PULLUP);
}
bool machineRunnig = false;
void loop() {
if (digitalRead(button_On) == LOW){
if (!machineRunnig) {
digitalWrite(Relay, HIGH);
delay(100);
digitalWrite(Relay,LOW);
Serial.println("Relay ON");
machineRunnig = true;
}
}
if (digitalRead(button_Off) == LOW){
if (machineRunnig) {
digitalWrite(Relay, LOW);
digitalWrite(Relay, HIGH);
delay(100);
digitalWrite(Relay, LOW);
Serial.println("Relay OFF");
machineRunnig = false;
}
}
}
here it is I would consider this to be solved. Arduino community is great! I have read through countless forums in my life and this is like no other! Thank you guys onto adding the RST and addalogger!