Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Serial Sketches for the Arduino

These Arduino projects are designed to display the value of inputs using the serial monitor. Serial is a method of communication between a peripheral and a computer. In this case, it is serial communication over Universal Serial Bus (USB). When reading sensors with an Arduino, the values are sent over this connection and can be monitored or interpreted on your computer.

The DigitalReadSerial Sketch

In the DigitalReadSerial project you monitor the HIGH and LOW values of a button over the serial monitor.

For this project, you need:

  • An Arduino Uno

  • A breadboard

  • A 10k ohm resistor

  • A pushbutton

  • Jump wires

    image0.jpg

Complete the circuit and upload the code from File→Examples→01.Basics→DigitalReadSerial.

image1.jpg
/*
 DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor
 This example code is in the public domain.
 */
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 // make the pushbutton's pin an input:
 pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
 // read the input pin:
 int buttonState = digitalRead(pushButton);
 // print out the state of the button:
 Serial.println(buttonState);
 delay(1);  // delay in between reads for stability
}

After you upload the sketch, click the serial monitor button on the top right of the Arduino window. Clicking this button opens the serial monitor window and displays any values being sent to the currently selected serial port.

In the window, you should see a cascade of 0 values. Press the button a few times, and you should see some 1 values appear.

image2.jpg

If you don’t see anything, or you see incorrect values, double-check your wiring:

  • Make sure that you’re using the correct pin number for your button.

  • If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.

  • If you are receiving strange characters instead of 0s and 1s, check the baud rate in the serial monitor; if it is not set to 9600, use the drop-down menu to select that rate.

The AnalogInOutSerial Sketch

In this project, you monitor an analog value sent by a variable resistor over the serial monitor. These variable resistors are the same as the volume control knobs on your stereo.

In this example, you monitor the value as detected by your Arduino and display it on your screen in the serial monitor, giving you a greater understanding of the range of values and performance of this analog sensor.

You need:

  • An Arduino Uno

  • A breadboard

  • A 10k ohm variable resistor

  • A resistor (greater than 120 ohm)

  • An LED

  • Jump wires

The circuit uses an LED connected to pin 9 as in the Fade circuit. The code fades the LED on and off according to the turn of the potentiometer.

Because the input and the output have a different range of values, the sketch needs to include a conversion to use the potentiometer to fade the LED. This is a great example of using the serial monitor for debugging and displays both the input and output values for maximum clarity.

image3.jpg

Complete the circuit and upload the code from File→Examples→03.Analog→AnalogInOutSerial.

image4.jpg
/*
 Analog input, analog output, serial output
 Reads an analog input pin, maps the result to a range from 0 to 255,
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground
 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe
 This example code is in the public domain.
 */
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is
        // attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;  // value read from the pot
int outputValue = 0;  // value output to the PWM (analog out)
void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
}
void loop() {
 // read the analog in value:
 sensorValue = analogRead(analogInPin);  
 // map it to the range of the analog out:
 outputValue = map(sensorValue, 0, 1023, 0, 255);
 // change the analog out value:
 analogWrite(analogOutPin, outputValue);  
 // print the results to the serial monitor:
 Serial.print("sensor = " );     
 Serial.print(sensorValue);
 Serial.print("\t output = ");
 Serial.println(outputValue);
 // wait 2 milliseconds before the next loop
 // for the analog-to-digital converter to settle
 // after the last reading:
 delay(2);    
}

After you upload the sketch, turn the potentiometer with your fingers. The result should be an LED that fades on and off depending on the value of the potentiometer. Now click the serial monitor button on the top right of the Arduino window to monitor the same values that you are receiving and sending to the LED.

If you don’t see anything happening, double-check your wiring:

  • Make sure that you’re using the correct pin number for your variable resistor.

  • Check that your LED is the correct way round, with the long leg connected to Pin 9 and the short leg in GND, via a resistor.

  • If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.

  • If you are receiving strange characters instead of words and numbers, check the baud rate in the serial monitor. If it is not set to 9600, use the drop-down menu to select that rate.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.