Products > Embedded Computing

Arduino with a relay and two obstacle sensors.

(1/2) > >>

geobee:
Hi all, trying to get my head around this. I need to trigger a relay from Arduino, from two IR sensors so that the first trigger latches the relay, then the second trigger from the other sensor, unlatches the relay. In other words, a toggle function. I have searched the web, and there are plenty of ''momentary'' relay drivers, but cannot find what suits. I am a total noob to Arduino coding. I can do simple sketch mods, but am still learning at a very low pace. Am into model trains, and have quite a few Arduino items that I would like to get going. Any help or sketch ideas would be a great help.

themadhippy:
I'd use something like

assign  a variable,
if   first trigger is high make  the variable  =1
if  second trigger from the other sensor is high  make  the variable 0
else variable = variable
set the output pin to the value of the variable

Ian.M:
If the sensors have a simple logic or dry contact output, and *ALL* you need to do is latch/unlatch the relay, you don't even need an Arduino.  Its a job for a SR flipflop which can be implemented with a single CMOS 4000 series logic IC + a driver transistor for the relay, or even by the BJT incarnation of the classic Eccles Jordan Bistable circuit, probably with an extra transistor to get strong relay drive.  See: https://en.wikipedia.org/wiki/Flip-flop_(electronics)

However if you *MUST* do it in code, it will probably look something like this:

--- Code: ---void loop(){
   while(!digitalRead(SENSOR1)){}; // wait for sensor 1 to activate
   digitalWrite(RELAY, ON);
   while(digitalRead(SENSOR1)){}; // wait till sensor 1 is inactive

   while(!digitalRead(SENSOR2)){}; // wait for sensor 2 to activate
   digitalWrite(RELAY, OFF);
   while(digitalRead(SENSOR2)){}; // wait till sensor 2 is inactive
}

--- End code ---
+ a bunch of #defines for the pin numbers for RELAY, SENSOR1, SENSOR2, and the logic levels for ON and OFF. It will also need some stuff in setup() to set the pin directions and whether or not to use input pullups.  Note that the above assumes active high sensor outputs. If they are active low, swap the ! in the while() expressions to the other pair of while() statements.
   
Themadhippy's idea of using a variable to store the relay state is necessary if you want a single Arduino to read multiple pairs of sensors to control multiple relays independently, looping through updating them each in turn fast enough you cant perceive the delay*, but it over-complicates the code if handling two sensors and a single relay is the Arduino's *ONLY* task. 

* You'd need to grok state machines and superloops which can be quite an adventure for less experienced programmers to make the multi-task version work well.

geobee:
Thanks to both, I should be able to get something happening from that. Cheers

brucehoult:

--- Quote from: geobee on July 04, 2021, 10:41:54 pm ---Hi all, trying to get my head around this. I need to trigger a relay from Arduino, from two IR sensors so that the first trigger latches the relay, then the second trigger from the other sensor, unlatches the relay. In other words, a toggle function. I have searched the web, and there are plenty of ''momentary'' relay drivers, but cannot find what suits. I am a total noob to Arduino coding. I can do simple sketch mods, but am still learning at a very low pace. Am into model trains, and have quite a few Arduino items that I would like to get going. Any help or sketch ideas would be a great help.

--- End quote ---

The good thing about doing things in code is you can implement arbitrary complex logic very easily.

Maybe the bad thing about it is you should specify what you want more precisely.

- is it possible for the IR sensors to give a continuous "object detected" output, or is it inherently momentary?

- if it is possible to be continuous, what do you want to happen if the second signal is received while the first is still active?

- what do you want to happen if the first IR detector triggers multiple times before the second one triggers? If the first one triggers three times (say) should a single trigger of the second one turn the relay off, or only after three triggers?

- should the relay change state at the positive edge of both detectors, or at the negative edge of one (or both) of them?

- if a detector can trigger several times rapidly, is there a time period within which multiple triggers should be considered to be just one?

- is there a minimum time the relay should be on or off for?

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod