User Tools

Site Tools


arduino:arduino_crash_course:serial_communication

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
arduino:arduino_crash_course:serial_communication [2012/09/14 01:18] – created mithatarduino:arduino_crash_course:serial_communication [2012/11/03 21:47] mithat
Line 1: Line 1:
 ====== Serial Communication ====== ====== Serial Communication ======
 +
 +Serial communication is a two-way stream of text characters that is communicated over a **serial port**. It is one way to let the Arduino board (called the **target**) communicate with the computer you are using to develop Arduino programs (called the **development host** or just **host**).
 +
 +Serial communication is often used to make the Arduino report back to the host the value of the program's variables or other information as the program is running. This can be of great value when you are trying to debug programs.
  
 ===== Switch controlled LED with serial reporting ===== ===== Switch controlled LED with serial reporting =====
 +
 +The following is the [[arduino:arduino_crash_course:basic_interaction#using_internal_pullups|switch-controlled LED]] example modifed to report the state of the pushbutton back to the host computer.
 +
 +Once you have uploaded the program to the Arduino and it is running, open the serial monitor using the IDE's //Tools > Serial Monitor// menu item. Notice the values that are reported in the Serial Monitor window when you press and unpress the switch.
 +
 <file c LightSwitchMonitored.ino> <file c LightSwitchMonitored.ino>
 /* /*
Line 7: Line 16:
  Turn an LED on and off and send switch state to serial monitor  Turn an LED on and off and send switch state to serial monitor
  */  */
- +
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
- +
 void setup() { void setup() {
   Serial.begin(9600);  // initialize serial communication at 9600 bits/second   Serial.begin(9600);  // initialize serial communication at 9600 bits/second
Line 22: Line 31:
 void loop() { void loop() {
   int buttonState = digitalRead(pushButtonPin);  // read the input pin   int buttonState = digitalRead(pushButtonPin);  // read the input pin
- +
   // set LED state accordingly   // set LED state accordingly
   if (buttonState == LOW)         // if the button is pushed   if (buttonState == LOW)         // if the button is pushed
Line 28: Line 37:
   else                            // otherwise   else                            // otherwise
     digitalWrite(ledPin, LOW);    // turn the LED off     digitalWrite(ledPin, LOW);    // turn the LED off
- +
   Serial.println(buttonState);    // print out state of button   Serial.println(buttonState);    // print out state of button
   delay(500);                     // delay between reads   delay(500);                     // delay between reads
 } }
 </file> </file>
 +
 +The official Arduino [[http://arduino.cc/en/Reference/Serial|Serial]] reference material.
arduino/arduino_crash_course/serial_communication.txt · Last modified: 2012/11/12 18:23 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki