Arduino Lesson 5. The Serial Monitor



Similar documents
Arduino Lesson 9. Sensing Light

Arduino Lesson 4. Eight LEDs and a Shift Register

Arduino Lesson 13. DC Motors. Created by Simon Monk

Arduino Lesson 1. Blink

Arduino Lesson 14. Servo Motors

Arduino Lesson 16. Stepper Motors

Lab 6 Introduction to Serial and Wireless Communication

Arduino Lesson 17. Sending Movement Detector

Arduino Lesson 0. Getting Started

Connecting Arduino to Processing a

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Microcontroller Programming Beginning with Arduino. Charlie Mooney

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

Sending an SMS with Temboo

Arduino Wifi shield And reciever. 5V adapter. Connecting wifi module on shield: Make sure the wifi unit is connected the following way on the shield:

SSH to BeagleBone Black over USB

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Arduino Internet Connectivity: Maintenance Manual Julian Ryan Draft No. 7 April 24, 2015

DS1307 Real Time Clock Breakout Board Kit

Sending Data from a computer to a microcontroller using a UART (Universal Asynchronous Receiver/Transmitter)

Programming the Arduino

Creating trouble-free numbering in Microsoft Word

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Playing sounds and using buttons with Raspberry Pi

Home Security System for Automatic Doors

Lecture 7: Programming for the Arduino

2.2" TFT Display. Created by Ladyada. Last updated on :15:09 PM EDT

Work with Arduino Hardware

Lesson 8: Simon - Arrays

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

Computer Architectures

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

GM862 Arduino Shield

LCD I 2 C/Serial RX Backpack. Data Sheet. LCD to I2C/Serial RX Backpack I2C or Serial RX communication with Standard 16 Pin LCD modules

Set up and Blink - Simulink with Arduino

Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

The Answer to the 14 Most Frequently Asked Modbus Questions

SUDT AccessPort TM Advanced Terminal / Monitor / Debugger Version 1.37 User Manual

Adafruit's Raspberry Pi Lesson 6. Using SSH

Driver Installation and Hyperterminal Operation of iload Digital USB Sensors

Adafruit's Raspberry Pi Lesson 3. Network Setup

Virtual Integrated Design Getting started with RS232 Hex Com Tool v6.0

How to Make a Pogo Pin Test Jig. Created by Tyler Cooper

E-Blocks Easy Internet Bundle

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Informatica e Sistemi in Tempo Reale

1 /*ora sincrinizzata con NTP. Inserendo codice OTP si attiva un servo che ruota di 95 grad. Per chiudere il servo premere 2 "*" mentre per azzerrare

TYPING IN ARABIC (WINDOWS XP)

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

1.8" TFT Display Breakout and Shield

CM HOST CM CardTransporter Fuel Communication and Management Software Software version up to 3.1

Time Clock Import Setup & Use

SupverVAG K+CAN User Manual

Create a Simple Website. Intel Easy Steps Intel Corporation All rights reserved.

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

PaymentNet Federal Card Solutions Cardholder FAQs

How To Connect A Raspberry Pi To The Internet On A Microsoft Moonstone 2 (Microsoft) Microsoft Powerbook 2 (Powerbook 2) (Powerboard 2) And Powerbook 3 (Powerstation 2)

CNC Transfer. Operating Manual

SAPScript. A Standard Text is a like our normal documents. In Standard Text, you can create standard documents like letters, articles etc

How to Move Canon EF Lenses. Yosuke Bando

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Using Windows Task Scheduler instead of the Backup Express Scheduler

IR Communication a learn.sparkfun.com tutorial

1602 LCD adopts standard 14 pins(no backlight) or 16pins(with backlight) interface, Instruction of each pin interface is as follows:

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

Macro s for EIS Using IBM 3270 Personal Communications Emulation Software

Arduino Shield Manual

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Audacity is a free, totally free, audio editing program. Get it here:

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

CENTRONICS interface and Parallel Printer Port LPT

Arduino-Based Dataloggers: Hardware and Software David R. Brooks Institute for Earth Science Research and Education V 1.2, June, , 2015

Android, Bluetooth and MIAC

2. Setting Up The Charts

Arduino Microcontroller Guide W. Durfee, University of Minnesota ver. oct-2011 Available on-line at

3. Programming the STM32F4-Discovery

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

J a v a Quiz (Unit 3, Test 0 Practice)

DiskPulse DISK CHANGE MONITOR

Raptor-CAN User Manual

Introduction to Java

Testing Data Radio Modem with Serial Port Tool V1.20

User s Guide for Polycom CX7000 Systems

Creating a Project with PSoC Designer

Additional Guides. TETRIX Getting Started Guide NXT Brick Guide

SYMETRIX SOLUTIONS: TECH TIP August 2015

Working with sections in Word

Deposit Direct. Getting Started Guide

Mini Amazing Box Update for Windows XP with Microsoft Service Pack 2

Creating Database Tables in Microsoft SQL Server

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

Transcription:

Arduino Lesson 5. The Serial Monitor Created by Simon Monk Last updated on 2013-06-22 08:00:27 PM EDT

Guide Contents Guide Contents Overview The Serial Monitor Arduino Code Other Things to Do 2 3 4 7 10 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 2 of 11

Overview In this lesson, you will build on lesson 4, adding the facility to control the LEDs from your computer using the Arduino Serial Monitor. The serial monitor is the 'tether' between the computer and your Arduino - it lets you send and receive text messages, handy for debugging and also controlling the Arduino from a keyboard! For example, you will be able to send commands from your computer to turn on LEDs. In this lesson, you will use exactly the same parts and a similar breadboard layout as Lesson 4. So, if you have not already done so, follow lesson 4 now. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 3 of 11

The Serial Monitor Upload the following sketch to your Arduino. Later on, we will see exactly how it works. /* Adafruit Arduino - Lesson 5. Serial Monitor */ int latchpin = 5; int clockpin = 6; int datapin = 4; byte leds = 0; void setup() pinmode(latchpin, OUTPUT); pinmode(datapin, OUTPUT); pinmode(clockpin, OUTPUT); Serial.begin(9600); while (! Serial); // Wait untilserial is ready - Leonardo Serial.println("Enter LED Number 0 to 7 or 'x' to clear"); void loop() if (Serial.available()) char ch = Serial.read(); if (ch >= '0' && ch <= '7') int led = ch - '0'; bitset(leds, led); Serial.print("Turned on LED "); Serial.println(led); if (ch == 'x') leds = 0; Serial.println("Cleared"); void updateshiftregister() digitalwrite(latchpin, LOW); shiftout(datapin, clockpin, LSBFIRST, leds); digitalwrite(latchpin, HIGH); Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 4 of 11

After you have uploaded this sketch onto your Arduino, click on the right-most button on the toolbar in the Arduino IDE. The button is circled below. The following window will open. This window is called the Serial Monitor and it is part of the Arduino IDE software. Its job is to allow you to both send messages from your computer to an Arduino board (over USB) and also to receive messages from the Arduino. The message Enter LED Number 0 to 9 or 'x' to clear has been sent by the Arduino, and it is telling us what commands we can send to the Arduino which is either to send the 'x' (to turn all the LEDs off) or the number of the LED you want to turn on (where 0 is the bottom LED, 1 is the next one up right up to 7 for the top LED). Try typing the following commands, into the top area of the Serial Monitor that is level with the Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 5 of 11

'Send' button. Press 'Send', after typing each of these characters: x 0 3 5 Typing x, will have no effect, if the LEDs are already all off, but as you enter each number, the corresponding LED should light and you will get a confirmation message from the Arduino board, so that the Serial Monitor will appear as shown below. You can see that I am about to press send after entering 'x' again. Do this and all the LEDs will turn off. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 6 of 11

Arduino Code As you might expect, the sketch is based on the sketch used in lesson 4. So, we will just cover the new bits here. You will find it useful to refer to the full sketch in your Arduino IDE. Firstly, in the 'setup' function, there are three new lines on the end: void setup() pinmode(latchpin, OUTPUT); pinmode(datapin, OUTPUT); pinmode(clockpin, OUTPUT); Serial.begin(9600); while (! Serial); // Wait until Serial is ready - Leonardo Serial.println("Enter LED Number 0 to 7 or 'x' to clear"); Firstly, we have the command 'Serial.begin(9600)'. This starts serial communication, so that the Arduino can send out commands through the USB connection. The value 9600 is called the 'baud rate' of the connection. This is how fast the data is to be sent. You can change this to a higher value, but you will also have to change the Arduio Serial monitor to the same value. We will discuss this later, but for now leave it at 9600. The line beginning with 'while' ensures that there is something at the other end of the USB connection for the Arduino to talk to before it starts sending messages. Otherwise, the message might be sent, but not displayed. This line is actually only necessary if you are using an Arduino Leonardo, because the Arduino Uno automatically resets the Arduino board when you open the Serial Monitor, whereas this does not happen with the Leonardo. The last of the new lines in 'setup' sends out the message that we see at the top of the serial monitor. The 'loop' function is where all the action happens: void loop() if (Serial.available()) char ch = Serial.read(); if (ch >= '0' && ch <= '7') int led = ch - '0'; bitset(leds, led); Serial.print("Turned on LED "); Serial.println(led); if (ch == 'x') Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 7 of 11

leds = 0; Serial.println("Cleared"); Everything that happens inside the loop is contained within an 'if' statement. So unless the call to the built-in Arduino function 'Serial.available()' is 'true' then nothing else will happen. Serial.available() will return 'true' if data has been send to the Arduino and is there ready to be processed. Incoming messages are held in what is called a buffer and Serial.available() returns true if that buffer is Not empty. If a message has been received, then its on to the next line of code: char ch = Serial.read(); This reads the next character from the buffer, and removes it from the buffer. It also assigns it to the variable 'ch'. The variable 'ch' is of type 'char' which stands for 'character' and as the name suggests, holds a single character. If you have followed the instructions in the prompt at the top of the Serial Monitor, then this character will either be a single digit number between 0 and 7 or the letter 'x'. The 'if' statement on the next line checks to see if it is a single digit by seeing if 'ch' is greater than or equal to the character '0' and less than or equal to the character '7'. It looks a little strange comparing characters in this way, but is perfectly acceptable. Each character is represented by a unique number, called its ASCII value. This means that when we compare characters using <= and >= it is actually the ASCII values that were being compared. If the test passes, then we come to the next line: int led = ch '0'; Now we are performing arithmetic on characters! We are subtracting the digit '0' from whatever digit was entered. So, if you typed '0' then '0' '0' will equal 0. If you typed '7' then '7' '0' will equal the number 7 because it is actually the ASCII values that are being used in the subtraction. Since that we know the number of the LED that we want to turn on, we just need to set that bit in the variable 'leds' and update the shift register. bitset(leds, led); The next two lines write back a confirmation message to the Serial Monitor. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 8 of 11

Serial.print("Turned on LED "); Serial.println(led); The first line uses Serial.print rather than Serial.println. The different between the two is that Serial.print does not start a new line after printing whatever is in its parameter. We use this in the first line, because we are printing the message in two parts. Firstly the general bit: 'Turned on LED ' and then the number of the LED. The number of the LED is held in an 'int' variable rather than being a text string. Serial.print can take either a text string enclosed in double-quotes, or an 'int' or for that matter pretty much any type of variable. After the 'if' statement that handles the case, when a single digit has been handled, there is a second 'if' statement that checks to see if 'ch' is the letter 'x'. if (ch == 'x') leds = 0; Serial.println("Cleared"); If it is, then it clears all the LEDs and sends a confirmation message. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 9 of 11

Other Things to Do We sent the character to control the LEDs separately, by clicking send after each character. However, it will work just as well, if you send all the character commands in a single line. Try entering the following into the Serial Monitor and clicking 'Send': x0246 Now, lets see just how fast our Arduino can communicate. Change the baud rate in the sketch from 9600 to 115200 and upload it to the board again. Then after opening the Serial Monitor select 115200 from the drop-down list. You should find that that everything still works. High speed communication not often necessary, so the baud rate of 9600 is often used, as many serial peripherals such as GPS modules will have this as a default baud rate, so it is something of a standard. Also, try mismatching the baud rate - use Serial.begin(9600) and select 57600 in the dropdown menu, for example. See what it looks like? All garbled? That's a mismatch. If you're ever getting strange data in the Serial monitor, triple check your baud rates and make sure they match! Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor Page 10 of 11

The Serial Monitor is also a great way of debugging a sketch that is misbehaving. Sometimes, when a sketch is not behaving how you think it should behave, placing Serial.println() statements in key places will help you see what is going on. About the Author Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil Genius (http://adafru.it/868) and Programming the Raspberry Pi (http://adafru.it/am5). Adafruit Industries Last Updated: 2013-06-22 08:00:29 PM EDT Page 11 of 11