digitalWrite is backwards?

I bought a generic starter kit that only contained 8 LEDs and 8 220ohm resistors, and I wanted to visually display the temperature, so I wired up two 4 bit display 'rows' of LEDs in order to display the temperature in binary. Problem is, using digitalWrite to set the pin to HIGH turns the LED OFF while setting it to LOW turns the LED ON, which from what I understand is backwards. Is my understanding flawed or did I do something wrong? Code (for testing) is as follows:

void setup()
{
  Serial.begin(9600);
  for (int x = 7; x <= 13; x++)
  {
    pinMode(x, OUTPUT);
    digitalWrite(x, LOW);
  }
  // At this point, all of the LEDs are ON even though they're set to LOW.
  pinMode(A0, INPUT); // thermistor
}

void loop()
{
/*  int value = analogRead(A0);
  float temp = analogToTemp(value);
  
  byte ones = (int)temp % 10;
  byte tens = (temp - ones) / 10;

  setTensLEDs(tens);
  setOnesLEDs(ones);
  
  Serial.println(temp);
  */

  for (int pin = 7; pin <= 13; pin++)
    digitalWrite(pin, LOW);
  
  // The LEDs are all still ON at this point even though they're set to LOW still.

  setTensLEDs(15); // This should turn all the LDs ON by digitalWrite'ing them all to HIGH but it instead them OFF.
  setOnesLEDs(15); // This should turn all the LDs ON by digitalWrite'ing them all to HIGH but it instead them OFF.

  delay(1000);
}

void setTensLEDs(byte digit)
{

  digitalWrite(11, bitRead(digit, 0));
  digitalWrite(12, bitRead(digit, 1));
  digitalWrite(13, bitRead(digit, 2));

}

void setOnesLEDs(byte digit)
{
  
  
  digitalWrite(7, bitRead(digit, 0));
  digitalWrite(8, bitRead(digit, 1));
  digitalWrite(9, bitRead(digit, 2));
  digitalWrite(10, bitRead(digit, 3));

}

float analogToTemp(int value)
{

  float Temp = log(10000.0 * ((1024.0 / value - 1))); 
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp))* Temp);
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  return Temp;

Many/most Arduino peripherals are active low, including LEDs, relays, switches, etc. writing 0 or LOW turns on the LED, relay, or whatever. It's the way it is.

Regards,
Ray L.

It depends on how you are wiring the LED, for example, if you were to provide a power source to the LED that is NOT from the arduino pin, then setting the pin low will cause it to sink current (become ground) and thus turning the LED on. However, if you are supplying power directly from the arduino pin and hooking the other pin of the LED to ground, then setting the pin high will cause the LED to go on.

Recap,
POWER -> LED -> I/O PIN = (LOW = ON)
I/O PIN -> LED -> GROUND = (HIGH = ON)

RayLivingston:
Many/most Arduino peripherals are active low, including LEDs, relays, switches, etc. writing 0 or LOW turns on the LED, relay, or whatever. It's the way it is.

Regards,
Ray L.

for most of the items this is indeed true, but not for a led.
From what i understand your led is now connected this way: 5+ -> led(+) -> led(-) -> arduino pin.
that means that de arduino pin has to sink to turn on the led ( go LOW )

When you connect your led the other way around: gnd <- led(-) <- led(+) <- arduino pin.
Now the arduino pin has to source 5v ( go HIGH)

Edit: Ps991 was faster :frowning:

Problem is, using digitalWrite to set the pin to HIGH turns the LED OFF

No the problem is that, you think that's a problem :slight_smile:

The real "problem" is that for some , mainly historical , reasons integration of software and hardware is being overlooked.

Both - software and hardware - MUST work together to be useful even in such trivial tasks as turning LED on/ off.

You won't go very far as a software guru only, you need to understand the hardware too, like it or not.

Prime example - check out how may "problems" are posted here with just connecting power and GROUND.

"That's hardware - not my job man."

Good luck.

Lycaon:
... I wired up two 4 bit display 'rows' of LEDs in order to display the temperature in binary.... Is my understanding flawed or did I do something wrong?

The real problem is that you did not show your wiring.

How to use this forum

I wired up two 4 bit display 'rows' of LEDs ...

What is a 4 bit display 'row'?