1

enter image description hereI just started working with Arduinos on Tinkercad in a school assignment and have non preexisting electrical knowledge. Tried to read up on the subject and I think I've gotten some of the facts straight but i still can't get them to communicate when using the optocoupler. It works fine with the code if i connect them to each other directly with the tx/rx pins.

//Sender code
char string[5] = {'h', 'e', 'l', 'l', 'o'};
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 Serial.write(string,5);
 delay(1000);
}
//Reciving arduino code
char text[10];
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 Serial.readBytes(text,5);
 Serial.println(text);
 delay(1000);
}

My guess is I've done something wrong connecting the receiving Arduino but i can't find whats wrong. Any feedback is immensely appreciated!

asked Dec 10, 2021 at 17:20
2
  • 1
    How about the schematic? You could invert the signal, or it's connected in a way that even 9600 bauds/s are too fast for it Commented Dec 10, 2021 at 17:26
  • Try this as you have a logic inversion. If the Arduino is connected to the cathode, connect it to the anode or visa versa. This will cause a double logic inversion and get the signals back to working. Commented Sep 7, 2022 at 22:00

2 Answers 2

1

Here is a diagram that I would suggest you to to try it out. The transmitting Arduino pin will turn the internal infrared LED on or off depend on the whether the pin is at LOW or HIGH. When the infrared LED is on, it will turn on the phototransistor, and produce a LOW at the receiving arduino pin.

The LED is optional but it gives you a visual clue on whether you are receiving the data or not.

schematic

simulate this circuit – Schematic created using CircuitLab

answered Dec 11, 2021 at 1:59
1

EDIT: Even though the OP may be long gone, I wanted to finish what I started with my response. enter image description here

This circuit works fine for me using two UNOs (and one UNO and a serial board for a PC). It is, essentially, the same as in the first response by @hcheung. I used 4.7K and 10K for R2/R4 and they worked fine. I tested it using this program running on both UNOs.

 #include <SoftwareSerial.h>
SoftwareSerial SSerial(4,5); // RX, TX
void setup() {
 Serial.begin(9600);
 SSerial.begin(9600); 
}
void loop() { 
 // display whatever comes in through software serial on the Arduino monitor
 if(SSerial.available()) {
 Serial.write(SSerial.read());
 }
 // send whatever comes in on serial to software serial
 if(Serial.available()) {
 SSerial.write(Serial.read()); 
 }
}

Note that the programs use software serial and both Tx and Rx are isolated.

If you use this for the hardware serial port on two UNOs, take care. First, don't hang things on 0/1 until after the program loads and how much you can get away with is unclear to me.

Second, if you use a simple program like this, on both UNOs, you will quickly see that the first bytes sent will ping-pong and causes problems.

 void setup() {
 Serial.begin(9600);
}
void loop() {
 if(Serial.available()){
 Serial.write(Serial.read());
 }
}

I note that the OP was only using one end.

Personally, I found several junk box 4N25s that would not work - that took me a bit of time to figure out. Finally, I don't see the point of optically isolating two UNOs as I did for example, but that is another story.

______ older msg is below _______________________________

A typical way of doing this is illustrated here . enter image description here

// *** I am leaving my original text below, but I want to back off of this a bit. I spent a few hours on this today and I am not happy with the results or my understanding. If I come to any worthwhile conclusions, I will post them. I do like getting to the bottom of things, but I was premature. It is simply not working the way I would expect when I get into 9600 speed and I need to investigate further. Yes, I am accounting for the UNO 0&1 involvement with USB.

// ***

The advantage is that you will not be inverting the line on the receiving end. The choice of R1 and R2 is important. I used R1=330 and R2=1K and transmission was fine at 9600 using a junk box 4N25. My testing was with a single UNO and after uploading a simple program.

See https://github.com/pepaslabs/Electronics/wiki/Designing-a-TTL-Serial-Opto-isolator for a good discussion on exactly this topic.

PS: As mentioned you should have submitted a schematic and you could have used a meter to see what voltages you were getting - those are good steps to understanding things.

answered Sep 7, 2022 at 16:34
2
  • You connected Uno2 RX connecting to GND in your diagram. Commented Sep 8, 2022 at 0:33
  • @hcheung Yes, thanks - very sloppy of me - corrected Commented Sep 8, 2022 at 14:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.