Go Down

Topic: Optocoupler code help needed (Read 538 times) previous topic - next topic

jnolta

I'm using an Optocoupler module to detect an object falling through the slot. I need a simple code that reads the 5V input from the module to momentarily activate a relay. I've tried using the 5V to power the relay module directly, but without success. Arduino coding is definitely the hardest part for me, so thanks for your help.

wvmarle

How fast does this object pass? You may need to use an interrupt to bit miss the event.

How long to activate the relay?

This module indeed can't power a relay. What you describe you may be able to do in hardware using a 555 timer.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

cedarlakeinstruments

#2
Sep 18, 2018, 01:28 pm Last Edit: Sep 18, 2018, 01:41 pm by cedarlakeinstruments
Opto input on pin 2, relay output on pin 3. Assumes relay driver is active HIGH and opto is active LOW.
Code: [Select]

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  while(1)
  {
     if (digitalInput(2) == LOW)
     {
        // Activate relay
        digitalWrite(3, HIGH);
        while(1);
      }
   }


PM me if you need more than this.
Electronics and firmware/software design and assistance. No project too small

jnolta

When I compile it, it says that (digitalInput(2) "was not declared in this scope". Does it need to be defined at the top of the sketch?

wvmarle

That command is of course simply wrong. Use digitalRead() instead.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

cedarlakeinstruments

Electronics and firmware/software design and assistance. No project too small

jnolta

Thank you very much for your help. I was able to modify it without too much effort to get it to do what I needed using the following code:



void setup() {
  pinMode(5, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);
 
 
      } // put your setup code here, to run once:



void loop() {
 
     if (digitalRead(5) == HIGH)
     {
        // Activate relay
        digitalWrite(6, LOW);
        delay (200);
        digitalWrite(6, HIGH);
     }
        else {
          digitalWrite(5, LOW);
        }
        }

This allowed the relay to switch every time the optocoupler slot is covered. I am building a "Plinko" game where a disk falls through the array of pins and through one of five slots. Four of the slots are "losers" and one is a "winner" I wired four of the "loser" sensors to one module and the "winner" sensor to a second module.  The code I have doesn't include the second module yet, I'll just add in another if/else series.

Thanks again for your help.

wvmarle

If you digitalWrite(pin, LOW) to an input pin, the pull-up resistor is disabled. I don't think you want to do this.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

jnolta

I'm completely ignorant, can you let me know why I wouldn't want to do this? The circuit is working exactly as I had intended using this code.

wvmarle

You make the pinMode(5, INPUT_PULLUP) in the setup, you probably have a reason for using that instead of just INPUT.

Then in in the loop() you do a digitalWrite(5, LOW) which doesn't make much sense in the first place as it's an input, but it does disable this pull-up resistor.

It's simply a bug. Maybe you intend to do something with pin 6 there? If so, it nicely demonstrates also why you should use names for pins rather than numbers.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

jnolta

Thanks for your response. What change would you suggest that will give me the same output/result?

wvmarle

You can probably drop that whole else statement but it depends on the results you get and the circuit.

It'd be actually more useful (and important) for you to actually try to understand what's actually going on in the code you posted. Why do you manipulate those two pins in the way you do?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up