0

I have made a very basic test circuit for the opto coupler H11L1 ('similar'datasheet: http://www.datasheet4u.com/datasheet/H/1/1/H11L1_MotorolaInc.pdf.html))

I use the test circuit:

schematic

simulate this circuit – Schematic created using CircuitLab

Circuit

The LED and 470 ohm resistor is only for testing. U1 is the Arduino (software serial pin 9/11) and U2 is the H11L1.

The LED is flashing similar to the RX led on the Arduino.

Also I tested before in an even simpler circuit that the signals from the H11L1 pass through pin 4 (although inverted, which seems to be normal).

However, I don't receive any data in the Arduino (I tried different Arduinos, different speeds, different H11L1).

The data I receive from the (hardware) serial/debug terminal:

0
0
0
0

etc.

My sketch:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 11); // RX, TX

void setup()
{
    Serial.begin(9600);
    mySerial.begin(300);
}

void loop()
{
    mySerial.write('A');
    while (mySerial.available())
    {
        Serial.println(mySerial.read());
    }

    delay(100);
}

How can I get the correct signal? (meaning 'A' are printed instead of 0's?

Update

New sketch (not having the duplex problem):

#include <SoftwareSerial.h>

SoftwareSerial mySerial(12, 13); // RX, TX

void setup()
{

  Serial.begin(115200);
  mySerial.begin(115200);
}

void loop()
{
  Serial.print("Write: A ");
  mySerial.write('A');

  while (mySerial.available())
  {
    Serial.print((int) mySerial.read());
  }

  Serial.println("");

  delay(100);
}

Output:

Write: A
Write: A
Write: A
Write: A

etc.

Update

Problem solved: I put it in an answer for clearity.

14
  • 1
    First, try looping back the Arduino's RX/TX lines to verify you do not have a full verses half duplex problem. – st2000 Jun 25 '17 at 13:16
  • 1
    I'm still looking around. When looping back, you are depending on the Atmel processor hardware to buffer up received data while your software is busy transmitting it. Then you are depending on the Arduino serial libraries to get that buffered data (if there is any) after the it was already received. It is like talking and listening at the same time. Instead of the usual taking then listening in turn. – st2000 Jun 25 '17 at 13:24
  • 1
    I am fairly sure the UART connected pins are similar or the same as GPIO. Interestingly, I can't find an exact match to this problem (if in fact it is a problem). Most use the UART to send messages followed by and EOL like an '\n". So they write sketches which expect to talk in turn. You, on the other hand, probably need to write a sketch which will work byte by byte. This assumes the Atmel processor you are using can only buffer 1 UART byte. Take a look at "Serial.readBytes()" to get some ideas. This is a long way to go just to test the optical coupler. – st2000 Jun 25 '17 at 13:35
  • 1
    Perhaps I should add. In the real world, I would write an interrupt routine to handle the received data. All it would do is maintain a buffer of received data that is long enough to contain any expected messages until I could get around to reading it. So when I'm off sending data I will not miss any data being sent to me. I am not sure how the Arduino serial libraries are written. But they need to be simple so as to be small and compatible with as many other Arduino libraries as possible. – st2000 Jun 25 '17 at 13:40
  • 1
    R2 and R3 form a voltage divider that prevents a proper high voltage; you have to connect the LED to 5V as shown in Transistor's answer. And the real MIDI circuit has more than one 220 Ω resistor; you can increase R1 to 1 kΩ. Anyway, for loopback testing, a direct connection 11 → 9 is OK. – CL. Jun 25 '17 at 14:14
3

schematic

simulate this circuit – Schematic created using CircuitLab

I'm not sure I can help much with the MIDI problem but I can help with the schematic. I've reorganised it for clarity. Since the opto-isolator has an open-collector output any loads it is driving should be connected to supply + rather than common.

11
  • 1
    With the LED only for testing, the actuall pull-up is missing. – CL. Jun 25 '17 at 14:08
  • 1
    Sure. He can put the actual pull-up back when finished testing. – Transistor Jun 25 '17 at 14:09
  • 1
    I like how @Transistor cleaned up your circuit. It should work. CL is correct, you had a voltage divider before and your test LED may have caused problems. Note, different Arduinos can use different processors with different internal hardware. Not all processor UARTs are alike. Some have more hardware buffering which off loads the burden of serial timing from the software. There is a balance between cost, hardware features and software complexity. Welcome to Firmware land. – st2000 Jun 25 '17 at 14:51
  • 1
    R3 and D1 provide the pull-up. The open-collector output will pull it down when turned on giving almost 5 V across the R3 - D1 combination. – Transistor Jun 25 '17 at 15:01
  • 1
    Yes, you've discovered the difference between software and firmware :)! It's the particular processor the board uses (Mega uses ATmega2560 while the Uno uses ATmega328P). Most copies use the same Atmel chip. But the bigger and more elaborate boards use complex chips. Some with ARM cores. It will be these differences in chips which may turn out to make your (high level) code work differently (better) in some cases. If you really need this (full duplex all the time?) to work, I would try that other serial library AltSoftSerial that @jot suggested if you can. – st2000 Jun 25 '17 at 20:18
2

Rule of thumb: never use SoftwareSerial (unless you have to, and even then you should avoid it).

The SoftwareSerial works better with higher baudrates. The lower the baudrate, the more trouble you get. Unless you aim for a high baudrate of 115200, then SoftwareSerial can cause troubles again. Remember that using SoftwareSerial means that there is not much else you can do in your sketch.

When SoftwareSerial is used with a very low baudrate of 300, then it could even disturb the receiving of data of other hardware serial ports. Because SoftwareSerial disables interrupts during transmitting and receiving, the lower the baudrate, the longer the interrupts will be disabled.

The SoftwareSerial can not transmit and receive at the same time, and that is what you are doing.

A Arduino Leonardo, Micro, or Arduino Mega 2560 board have unused hardware serial port(s). Use one of those to do tests with a serial port. As soon as you start using SoftwareSerial for a test, you are actually testing the SoftwareSerial itself.

There is an alternative, the AltSoftSerial Library. it can receive and transmit at the same time. Use a baudrate that is not too high, 9600 baud should be fine.

6
  • I will try with higher baudrates ... also I changed my sketch (but without much improvement). To be honest, I have a Mega, but I don't 'dare' to use it, since that one is now on a breadboard circuit that I'm for sure knows it works (and by disassembling it, I'm afraid I don't have any working solution). I have ordered a new one, and will test it again when I receive it. – Michel Keijzers Jun 25 '17 at 13:32
  • 1
    To the best of my knowledge, “Software Serial works better with higher baudrates. The lower the baudrate, the more trouble you get” is false. In general, software serial is incapable of handling data rates like 115200 without high incidence of errors. OTOH, it should handle 4800, 9600, etc ok – James Waldby - jwpat7 Jun 25 '17 at 13:32
  • MichelKeijzers, The SoftwareSerial is useless for you, it can not transmit and receive at the same time. @JamesWaldby With lower baudrates, the interrupts are disabled for longer time. With a full sketch and lots of things going on, it will cause a lot of trouble for sure. – Jot Jun 25 '17 at 13:35
  • I tried with 300, 9600 and 115200 (no changes) ... probably the duplex is the main problem ... I will try this test again when I have the new Mega. – Michel Keijzers Jun 25 '17 at 13:38
  • 1
    In the Arduino IDE, in the Library Manager, search for: altsoft. Install the "AltSoftSerial by Paul Stoffregen". Find which fixed pins are for RX and TX ( pjrc.com/teensy/td_libs_AltSoftSerial.html ). Use 9600 baud, and it should work. – Jot Jun 25 '17 at 13:43
0

Problem solved:

I created a setup with two Arduino Uno's, one which sends the information by a hardware UART, one that receives it. The reason to do this is not to use a software serial and having no problems with the duplex. I thought I need a Mega, but with two Uno's it is also possible.

However, this did NOT solve the problem.

What solved the problem was to use TWO opto-isolators. The H11L1 inverts the signal, and it seems I had to connect the output of the first H11L1 to the input of a second and now it works.

3
  • 1
    Or invert the connection of the optocoupler's LED (anode to power, cathode to TX), like in the MIDI circuit. – CL. Jun 26 '17 at 6:28
  • Oh of course (thought it was in sending, but if inverting that way is possible, than it's even better). Just doubting if the MIDI functionality will work (since it will be functionally similar as now) :-( – Michel Keijzers Jun 26 '17 at 7:44
  • @CL: Thanks for your remark ... this was the solution .... now I can finally replace my 6N137s by H11L1's ... thanks for your remark and patience (same for all others who helped me). – Michel Keijzers Jun 26 '17 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.