Arduino with a relay and two obstacle sensors. - Page 1
Forum section: electronics / general

author: geobee
author_rank: Contributor
timestamp: July 04, 2021, 10:41:54 pm
content: 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.

author: themadhippy
author_rank: Super Contributor
timestamp: July 04, 2021, 11:18:52 pm
content: I'd use something likeassign a variable,if first trigger is high make the variable =1if second trigger from the other sensor is high make the variable 0else variable = variableset the output pin to the value of the variable

author: Ian.M
author_rank: Super Contributor
timestamp: July 04, 2021, 11:57:10 pm
content: 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 activatedigitalWrite(RELAY, ON);while(digitalRead(SENSOR1)){}; // wait till sensor 1 is inactivewhile(!digitalRead(SENSOR2)){}; // wait for sensor 2 to activatedigitalWrite(RELAY, OFF);while(digitalRead(SENSOR2)){}; // wait till sensor 2 is inactive}+ 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.

author: geobee
author_rank: Contributor
timestamp: July 05, 2021, 02:44:37 am
content: Thanks to both, I should be able to get something happening from that. Cheers

author: brucehoult
author_rank: Super Contributor
timestamp: July 05, 2021, 04:21:17 am
content: Quote from: geobee on July 04, 2021, 10:41:54 pmThe 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?

author: geobee
author_rank: Contributor
timestamp: July 05, 2021, 09:59:36 am
content: Hi, it needs to latch the relay when the first sensor is triggered, then unlatch it when the second one is triggered, and so forth.

author: brucehoult
author_rank: Super Contributor
timestamp: July 05, 2021, 11:55:38 am
content: Yes, you said that in the first message.

author: Ian.M
author_rank: Super Contributor
timestamp: July 05, 2021, 12:22:32 pm
content: Quote from: geobee on July 05, 2021, 09:59:36 am@geobee,Perhaps if I explain the assumptions I made (for the code in reply #2) you can see how they differ from what you just stated.My assumptions:The sensors output an active high pulse when triggered long enough for the Arduino to easily respond to. The maximum pulse duration is unknown.The initial (power-up or after reset) state of the relay is off.The relay should turn on when the first sensor becomes (or is) active, remaining on while the first sensor is still active.Once the first sensor goes inactive, the relay should turn off when the second sensor becomes (or is) active, remaining off while the second sensor is still active.Once the second sensor goes inactive, repeat ad nauseam.That implies that if both sensors are active at the same time (i.e. their pulses overlap), whichever came first initially 'wins' then as they become inactive, whichever is last to become inactive 'wins'.I haven't even covered any requirement for debouncing. If you were dealing with mechanical switches or interrupted beam IR sensors you might choose to specify *either* 'Any change of sensor state shall be ignored until it has been stable for 10 ms.' *or* 'A change of sensor state shall be acted on immediately then further changes for the same sensor ignored until it has been stable for 10 ms, then acted on if required' or similar with a different debouncing period.As you can see, specifications for code in human language can get rather complex if you need to nail down all ambiguities.