hi i am playing around with optocoupler to supply input signal to uno.
i am using the blink sketch to output and digiralRead on same board to detect input.
if i Serial.print after digitalRead the digitalRead does not work i.e. input is hi always.
if i put a Serial.print brfore digitalRead the input changes.
i have a copy of sketch and wiring, can someone advice me how to up load documents
yes that's needed (optocouplers require a current limiting resistor, do you have one?)
Read this: 'How to get the best out of this forum' - it's all in there...
Upload, by simply dragging the image file, into the reply window.
For uploading code see How to get the best out of this forum
yes i have a resistor. i also have a led connected to output of optocupler to show device is working. my problem is the sketch and the location of "Serial.println(" low ");"
if i put this line before "Serial.println(digitalRead(ledin));" the print is correct.
if i put "Serial.println(" low ");" after, the print is 1 always
post everything so we can have a look
unable to load attachment as i am classed as a new uder
ah keep posting a bit and you'll be granted the rights ![]()
you should already be able to copy and paste the code here (make sure you use code tags and indent the code before posting here)
this is the code that i am using. take note where *************
int ledin = 2;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledin, INPUT);
Serial.begin(9600);
Serial.print(" photo optic test ");
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
//Serial.println(" high ");
Serial.println(digitalRead(ledin));
Serial.println(" high ");
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
Serial.println(" low "); // turn print on digitalRead work*************
Serial.println(digitalRead(ledin));
//Serial.println(" low ");
delay(1000); // wait for a second
}
I edited your post and added the code tags and the indentation...
Please do yourself a favour and please read How to get the best out of this forum and post accordingly in the future.
--
can you describe with words what the circuit / power looks like?
output from uno pin 13 though 22o ohm to pin 1 optocoupler and pin 2 to earth.
5v to pin 5 optocoupler and pin 6 though 10k to earth. pin 6 optocoupler to uno pin 2.
also pin 6 optocoupler though 220 ohms and led to GND. led is on when pin 13 led is on, this tells me the optocoupler is working. sketch will work if i put a print in a spot and does not work if i remove the print
what's the reference of your optocoupler?
looks like this ?

that looks correct. 4N25 is number
Your code is missing
pinMode(13, OUTPUT);
in the setup function.
code is a modified version of blink code from example file.
pinMode(LED_BUILTIN, OUTPUT); i am on the understanding that this is pin 13 by default.
if the led and resistor is connected to pin 13, led will turn on and off at a control rate.
if i try to define this, code will error
That depends on the processor.
What Arduino are you using?
i am using UNO
Which UNO - the R4 is very different from R3 and earlier ...
i am using arduino UNO R3.
if i wire to match your diagram, i get no input at all.
the way i had it i could get an input.
my question is why does the Serial.println(" low "); line effect the Serial.println(digitalRead(ledin)); line. if print is before read, code works and if print is after read, code does not work.
