Arduino built in example: "Dimmer" not responsive to serial data

Viewed 18

Following this tutorial: Arduino LED Dimmer Example

I added the Serial.println(brightness); at the bottom to see what gets written to the brightness variable and only get back 48 no matter the value sent by serial to the arduino.

As you can see the value of brightness goes from 50 to 48 where it settles. The sent data is in pink and the black data is the response from the arduino.

What else could I try. Thanks

Using the Hercules utility to establish a serial link to the arduino. Hercules Utility Showing communication with Arduino

The oscilloscope sees this:

PWM signal from oscilloscope

const int ledPin = 9;      // the pin that the LED is attached to
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);

}


void loop() {
  byte brightness;
  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
    Serial.println(brightness);

 }
}
2 Answers

your problem is obvious in the image you provided , for example in the image it shows when send 100 , the arduino responded with 49 , 48 , 48 which is the ascii representation of the 100 , refer to ascii table where

'1' is equivalent to 49

'0' is equivalent to 48

which means that you send to the arduino the number 100 not as a number but as a string where your terminal sends 1 first in its ascii representation which is 49 then it sends 0 in its ascii representation which is 48 then it sends another 0 in its ascii representation which is 48.

to solve this problem:

in your pc side , try to change that in your terminal settings

or

in your arduino code , instead of brightness = Serial.read(); write brightness = Serial.parseInt(); , refer to Serial.parseInt() docs where it says that :

Looks for the next valid integer in the incoming serial. The function terminates if it times out

meaning that it will convert "100" (string) into 100 (number) in your arduino side .

also change byte brightness; into int brightness; as Serial.parseInt() returns the next valid int

Thank you abdo salm, your suggestion educated me and resolved my problem at the same time. Answer updated for anyone who stumbles across the same rookie issue (see snippet below).

So in conclusion, the example code expected a number in the range of 00000000 to 11111111 (0 to 255 as a single binary byte)

I was instead sending multiple bytes that represented One, Zero and Zero on the ASCII table

11000100 (49)
11000000 (48)
11000000 (48)

erroneously thinking I was sending 100 which is 110010000 in binary.

I altered the code with brightness = Serial.parseInt(); as abdo salm suggested.

Here is the Hercules (or similar) friendly version of the Arduino Dimmer Example.

const int ledPin = 9;      // the pin that the LED is attached to
int brightness;

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.parseInt();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
    Serial.println(brightness);
  }
}

Here is the new Hercules response.

And the scope changes as expected.

Related