6.1 Debugging with serial communication

Uncategorized — roguescience @ 5:56 pm

To figure out why our the LED is not behaving properly we need to be able get information from the Arduino while it is running to see what is going on.

Communication Protocols

Communication protocols were developed so that machines could send and receive data from other machines. Each protocol is made up of a set of standards that define how data is represented.

Can you name any communication protocols?

The communication protocol the Arduino uses is RS-232 serial protocol.

RS-232 serial protocol is an older well established protocol (from the late 1960s) that has been largely been replaced by faster and more advanced protocols like USB. However, it is still widely used via software emulation - in large part due to it’s simpler implementation. Setting up a serial connection involves specifying a port (COM on windows, /dev/ Linux, OS X) and a communication speed for each device or machine.

Let’s review the following code to see how serial communication is implemented for the Arduino.

/*
 * Debugging pot fader using serial communication
 * roguescience.org
 *
 * Fades a LED connected to digital pin 11 using Pulse Width Modulation (PWM)
 * based on input from analog pin 0 and sends values using serial communication.
 */

int ledPin = 11;                //choose the pin for the LED - needs to be (3,5,6,9,10, or 11)
int buttonPin = 2;               //choose the input pin for a pushbutton
int potPin = 0;                  //choose the input pin for a potentometer
int buttonVal = 0;                    //variable for reading the button status
int bounceCheck = 0;            //variable for debouncing
int potVal = 0;                //variable for reading potentiometer value

void setup() {
  pinMode(ledPin, OUTPUT);      //declare LED as output
  pinMode(buttonPin, INPUT);     //declare pushbutton as input
  Serial.begin(9600);            //begin serial communication at 9600 baud
}

void loop(){

  /*
  buttonVal = digitalRead(buttonPin);     //read input value from button
  delay(10);                              //wait 10ms
  bounceCheck = digitalRead(buttonPin);   //check again
  if(buttonVal == bounceCheck){           //if val is the same then not a bounce
    if (buttonVal == HIGH) {              //check if the input is HIGH
      digitalWrite(ledPin, LOW);         //turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);       //turn LED ON
    }
  }
  */

  potVal = analogRead(potPin);         //read input from potentiometer
  Serial.println(potVal);                   //send potVal via serial
  analogWrite(ledPin, potVal);        //use PWM to set LED brightness

}

Ok. Now cut and paste it into the Arduino software and then compile and upload it to your Arduino.

To see the serial data being sent to from the Arduino select the Serial Monitor button (the last button in the toolbar) and set the baud rate to the same rate the Arduino is communication on - in this case 9600.

You should now see the values that are being written to the analogWrite() function. Now can you figure out why the LED is fading in and out 4 times? Remember that the analogWrite() function expects a value between 0 and 255.

So how do we fix this? One way would be to normalize the data between 0 and 1 and then remap it to 0-255. Don’t know how to normalize data? Not a problem - the Arduino has a built in function that will do it for us. In the next part we will look at the map() function.

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2010 Roguescience Arduino Tutorials | powered by WordPress with Barecity