2
votes

I've written a code last year which was working well at that time. However on loading the same code this time I am getting reversed output. That is, when the Digital Pin is set to HIGH, it return LOW and vice versa.

digitalWrite(led, HIGH) //PROBLEM: Should turn ON the LED but insted it turns OFF

I've tried the BLINK EXAMPLE and in that case also the output seem to be reversed.

Here is the code:

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
// initialize the digital pin as an output.
pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(1000);               // wait for a second
digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
delay(5000);               // wait for a second
}

According to the code, my LED should be turned ON for 1 second and then should be turned OFF for 5 seconds before it again turns ON. However, the OUTPUT I am getting is completely reverse i.e LED is ON for 5 seconds and OFF for 1 second. I need help how to fix this.

My main code is based on interfacing arduino with android. I have been working on to fix this for android-arduino connectivity via bluetooth, which I did accomplish last year but now have encountered this issue. I have tried this on three different arduino uno boards, tried with different sensors but the HIGH-LOW seems to be reversed.

3
Are you sure you connection of the diode is not different from last year. In otherwords you could be driving the high side of the led with the output pin. Or you could be sinking the led with a low on the output pin.mpflaga
No. Connections are same as earlier. The high side of LED is pinned to +3.3V and the other side to digital pin no. 13. I was unable to trace out the problem with high number of connections so I decided to try it out on single LED, where i came to know that the digital output seems to be corrupted(reversed). Plus I've tried out on three different boards. Same problem.rohan.k
It appears as if it is working as expected. As several of us have eluded to, and you have confirmed you are low side driving the cathode(-) of the LED. Which will illuminate the LED when the Digital Output is LOW or zero.mpflaga

3 Answers

2
votes

Put a volt meter or oscilloscope on the output pin. Is LOW at 0 volts and HIGH at 5 volts? If so, the software and chip are working fine and your LED is wired so the other side is going to voltage, which means 0 V from the chip will turn it on. It's a common way to drive LEDs since some chips can sink more current than they can source.

0
votes

first of all, as @dithermaster wisely suggested you should check whether you're putting the LEDs in the right direction. The flat side (shortest pin) should always be plugged into the - pin and the rounded side (longest pin) to the + pin.

Never wire an LED between two IO pins, because it may harm the Atmega!

So basically, you want to have the + pin on pin led and the - pin on GND.

That said, if you say you changed nothing, and you've tried with different boards, your problem is indeed very strange…

So, I'm now wondering if your Arduino library is not corrupted in some way… Maybe did you hack something in the code that inverted HIGH and LOW a while ago and forgot about it?

To help you debug it, could you try a few things:

1/ can you change HIGH for 1 and LOW for 0, in the digitalWrite() function, and see if the behavior did change or not?

2/ if it did change, can you try the following

please could you run this sketch and tell us if it's behaving as expected? Have a look at the arduino monitor

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.print("HIGH: ");
    Serial.print(HIGH);
    Serial.print(" ; LOW: ");
    Serial.println(LOW);
}

3/ can you try changing digitalWrite()

with the following:

void loop() {
    out = portOutputRegister(digitalPinToPort(pin));
    *out |= digitalPinToBitMask(pin);  // set pin to HIGH
    delay(1000);
    *out &= ~digitalPinToBitMask(pin); // set pin to LOW
    delay(5000);
}

4/ I guess, you may also want to uninstall/reinstall the full Arduino framework, to have a fresh restart.

N.B.: what I'm trying to guess here, from 1. to 3. is that either someone inverted HIGH and LOW, or the way digitalWrite() behaves with HIGH and LOW. 4. is the desperate solution :-)

0
votes

The only time I have encountered this was after I messed around with the boards.txt file of arduino, if you have changed anything in this file you could uninstall and delete all arduino leftover data in C:/Program Files/Arduino just back up libraries and other scripts. Then reinstall arduino and it should be fine. Arduino can also be installed from windows store on win10 to have it auto update