-1

I want to write a program that allows me to control the LED connected to pin 13 of the Arduino. When the program is started, the LED should be off. The user should open the serial monitor to communicate with the Arduino. If the user sends the character '1' through the serial monitor then the LED should turn on. If the user sends the character '0' through the serial monitor then the LED should turn off.

So I tried:

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  int bytenum = Serial.available();
  if (bytenum != 0)
  {
    int bval = Serial.read();
    if (bval != 0)
    {
        digitalWrite(13, HIGH);
    }
  }
  digitalWrite(13, LOW);
}

But when launching it the LED never light up:

enter image description here

2
  • Serial Monitor sends a character '0' with ASCII code 48. so 48 is not equal to 0. for other shortcomings read Michel's answer
    – Juraj
    Commented Feb 10, 2021 at 19:36
  • Do you use real hardware or just a simulator like shown above? If you use real hardware, most Arduino UNOs have a build in LED at pin 13; no external LED is needed. If you use a simulator the ext. LED might be needed. The LED must be connected the right way anode to pin 13 and cathode (the side with the ring) to GND, did you check that? Michel explained you how to code it correctly and gve you tips how to connect an external LED, if your Arduino is missing the builtin LED or you wnat to try a different pin on real hardware. Commented Feb 12, 2021 at 7:02

1 Answer 1

0

I see two issues:

1. Sketch

The line:

digitalWrite(13, LOW);

is always executed, meaning the LED (which might be broken, see below), is on for a very small time, probably not enough to be visible.

You can use the next sketch (for 0 and 1 to switch it off/on):

const uint8_t LED = 13;

void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

void loop()
{
  while (available_characters != 0)
  {
    switch (Serial.read())
    {
      case '0':
        digitalWrite(LED, LOW); 
        break;
        
      case '1': 
        digitalWrite(LED, HIGH);
        break;
        
      default:
        // Ignore
        break;
    }
  }
}

You can make this sketch smaller in case you want '0' for off, and anything else for on:

const uint8_t LED = 13;

void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

void loop()
{
  while (Serial.available() != 0)
  {
    digitalWrite(LED, Serial.read() == '0' ? LOW : HIGH);
  }
}

2. Lack of a resistor

I think your LED is now broken, and hopefully only the LED and not your Arduino.

Always use a resistor just before or after the LED when connecting a LED. As a guideline, use e.g. 220R or 330R.

Remember as formula: V = I * R

The tricky thing is that LEDs have a forward voltage (Vf), so the actual formula is:

 (V - Vf) = I * R

Now most LEDs can handle mostly 20 mA, but 10 mA is more than enough to make them bright.

Below is a list of typical values for the forward voltage. Let's assume it's 1.8V as you are using a red one in your example.

enter image description here

The MINIMUM resistor value to be used is:

(5 V - 1.8 V) = 0.01 A * R <=>
3.2 V = 0.01 A * R <=>
R = 320 ohm

So use a 330 ohm resistor. (note that 220 ohm will still result in less than 20 mA).

Your Answer

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

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