Skip to main content

You are here

SOLVED: Arduino 1.0.1 breaks my opto coupler code

6 posts / 0 new
Last post
SOLVED: Arduino 1.0.1 breaks my opto coupler code


I have an optocoupler input in my jeenode and that works now for months. But after upgrading to Arduino 1.0.1 (same lastest jeelib library) i have a weird problem.

I have the following code:
int optoPort1Tmp = optoIn.digiRead();

With arduino 1.0 it returns always a 1, and thats correct.
With arduino 1.0.1 it randomy generates a 1 or 0, and thats not correct.

You can easily test this without optocoupler with the attached code. The first 5 or 6 reads are correct, then its generate randomly a fault value.

Opto.ino592 bytes

Verified. But if you swap like this, your code seems to work again.

optoIn.mode(INPUT); optoIn.digiWrite(1); optoIn.mode2(INPUT); optoIn.digiWrite2(1);

Thanks you. Looks like this works.

But i can't understand why this change works..


You'd have to look through the changes to the pinMode code on Arduino 1.0.1. I'll bet they changed it to set the output value to 0 before (or directly after) changing the mode to INPUT, to always start with disabled pull-ups. A sensible choice I think: when setting a new mode, you'll always start in the same state---OFF when it's an output, NO_PULLUPS when it's an input. This seems the most safe state.


The sequencing described by padvinder is important.
The ATmega spec, Section 14.2.3 Switching Between Input and Output list a series of rules for correct I/O pin configuration in the general case.


Ah, I found the culprit: the patch for feature request #246 indeed changed the definition of pinMode, as can be seen in this diff. As is now also described on the Arduino reference page for pinMode, the mode INPUT now disables the pull-up by default.

The fix you did worked, because digitalWrite(1) on an INPUT-mode pin enables the pull-up, whereas the other way round you make the pin HIGH, then set the mode which turns off the pull-up again. Semantically, an even better solution is now to use the new pin mode and leave out the digitalWrite altogether: optoIn.mode(INPUT_PULLUP);

Premium Drupal Themes by Adaptivethemes