LED not Switching ON/OFF

Hi All

Why will my LEDs not switch ON or OFF?

I am trying to set up the logic to use an HC-05 as a controller. The aim is to send two command strings via the HC-05 to switch a pinout from LOW to HIGH. The RISING will trigger a time-stamp on a separate board.

In my test scenario, I am using:

  • send string “Test1” to HC-05, this turns LED ON using digitalWrite(ledPin, HIGH);
  • send string “Test2” to HC-05, this turns LED OFF using digitalWrite(ledPin, LOW);

Problem is: the LED does not go HIGH or LOW.

On the bread board, the LED circuit is: Pin-10=>220ohm=>long-leg of LED=>short-leg of LED to GND. [The two attached image show the breadboard circuits.]

I have checked the circuitry:

  • my ammeter is checked against a flashlight and its batteries
  • with the ammeter directly bridging Pin-10 and GND, it shows NO voltage when digitalWrite is HIGH
  • the ammeter shows 220ohms in circuit from Pin-10 to the long-leg of LED
  • the LED is tested to work.

The RX[HC-06] to TX[UNO] goes through a voltage bridge, 2K and 1K resistors, reducing the Voltage at Pin-6[TX] to 3.3V. [See image attached.]

The serial output shows that the text inputs run through the code where digitalWrite(ledPin, LOW) and digitalWrite(ledPin, HIGH) are executed. This is my serial output:

Test1
LED: ON
readString: Test1: 5
Test1
LED: ON
readString: Test1: 5

Here is my full sketch. (Please note: The code has a LOT of steps in it to help me debug - and understand what’s going on.)

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 6); // RX | TX // Same as other tests

#define ledPin 10
char c = "";
String readString = "";

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  delay(1000);
  digitalWrite(ledPin, HIGH); // Assumed the LED should go ON
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
  // Serial.begin(38400); // Default communication rate of the HC-05
  Serial.begin(9600); // EGB! Prduces readable output on Serial.println
  // BTSerial.begin(38400);  //  HC-05 default speed in AT command more
  BTSerial.begin(9600);  //  HC-05 default speed in AT command more
}
void loop() {

// -- Serial ---------------------------------------------------
  if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    while(Serial.available() > 0) { // While there is more, keep reading.
      delay(3);                    // wait 100ms for next reading
      c = Serial.read(); //Conduct a serial read
      readString += c; //build the string.
      Serial.print(c); 
    }
    Serial.println(""); 
  }
  delay(100);                    // wait 100ms for next reading

// -- Bluetooth ---------------------------------------------------
  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTSerial.available() > 0) {
    while(BTSerial.available() > 0) { // While there is more, keep reading.
      delay(3);
      // Serial.write(BTSerial.read());
      c = BTSerial.read(); //Conduct a serial read
      readString += c; //build the string.
      Serial.write(c); 
    }
    Serial.write("\n");
  }
  delay(100);                    // wait 100ms for next reading

// -- LEDs ON/OFF ---------------------------------------------------
  if (readString == "Test2") {
    digitalWrite(ledPin, LOW); // Turns LED OFF
    Serial.println("LED: OFF"); 
  }
  if (readString == "Test1") {
    digitalWrite(ledPin, HIGH);  // Turn LED ON
    Serial.println("LED: ON");;
  } 
  delay(100);                    // wait 100ms for next reading

// -- Validate String-------------------------------------------------
  if(readString.length() > 0){ // Checks whether data is comming from the serial port
    int j = readString.length(); // 
    Serial.print("readString: " + readString + ": "); // 
    Serial.print(j); // 
    Serial.println(""); // 
  }
  readString="";  //Reset the variable
  delay(100);                    // wait 100ms for next reading
}

So, why does my LED not switch ON or OFF?

I Look forward to your reply. Thanks in advance
EGB

HC-05_ATMode_Connections_to_Arduino.jpg

Hello there!

EGB: I have checked the circuitry: - my ammeter is checked against a flashlight and its batteries - with the ammeter directly bridging Pin-10 and GND, it shows NO voltage when digitalWrite is HIGH - the ammeter shows 220ohms in circuit from Pin-10 to the long-leg of LED - the LED is tested to work.

May I first ask why you are using an ammeter to read voltage and resistance? Are you using the term ammeter to describe a device that can measure all three?

That being said, I believe the issue may lie in your if statement conditions comparing the strings. If I remember correctly, the "==" operator does not function on strings as it does on integers. In regular c++ there is a command called strcmp(string1,string2) that is used to compare two strings. Maybe that will help.

I accept that your codes might make a lot of meanings. When something is not working, it is a good idea to write minimum codes (just as much as is needed) to test the functionality of the system. You can try the following codes (tested on UNO) where A is sent from Android Smartphone to make the LED ON and B is sent to make the LED OFF.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(5, 6); // RX | TX
#define ledPin 13    //built-in LED (L) of UNO

void setup()
{
  Serial.begin(9600);
  BTserial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  if (BTserial.available() > 0)
  {
    char c = BTserial.read();
    if (c == 'A')
    {
      digitalWrite(ledPin, HIGH);
    }
    if (c == 'B')
    {
      digitalWrite(ledPin, LOW);
    }
  }
}

bos1714: ... an ammeter to read voltage and resistance? ... conditions comparing the strings. If I remember correctly, the "==" operator does not function on strings as it does on integers. In regular c++ there is a command called strcmp(string1,string2) that is used to compare two strings. Maybe that will help.

Hey there, bos1714.

Thanks for these ideas. - I have one of those multipurpose "ammeters". Sorry for raising dread in you - frying Uno boards! - The "==" is a valid string compare. And, my logic went through that if-condition.

SOLVED: The problems was the circuitry - crumby breadboards, perhaps.!!!!

GolamMostafa: ... When something is not working, it is a good idea to write minimum codes ... to test the functionality of the system.

Thank you, GolamMostafa. Your advice about keeping it simple is really good. Simplifying it even further, I plugged the LED into pin13 and GND, which avoided the circuitry on my breadboard. THEN IT WORKED!

So the problem was the circuit to/from the LED on the breadboard.

Thank you. EGB

SOLVED

If I remember correctly, the "==" operator does not function on strings

that is correct, but it does work with Strings (uppercase S)