Using the Arduino serial console to monitor your Sketch

By: (plus.google.com) +David Herron; Date: July 6, 2017

Tags: Arduino

The most useful debugging tool many programmers have is the Print statement. They insert Print statements throughout their code, to see if the program got to certain points and if so what was the values. While there are fancy schmancy debugger packages available, the Print statement is extremely useful and simple. An Arduino presents a challenge because the software executes over on that teensy board, there's no computer display, no direct connection, where would the print statement send its output? Turns out the Arduino software stack includes a Serial console that you can view from inside the Arduino IDE. What we'll do in this article is go over a trivial example of reading an analog value to print on the serial console.

Built-in to the Arduino IDE is the capability for a Sketch to output through the USB connection to the Arduino serial console in the IDE.

Click this button to open the serial console
The serial console window. It displays text received from the Arduino, and apparently allows you to type lines of text to the Arduino

The Arduino Playground (playground.arduino.cc) has more ideas on interfacing an Arduino to the Serial port on another computer to interact with software on an Arduino. For our purposes, the simple Serial Console provides an easy way to get messages out of an Arduino.

To explore this we'll set up a simple Arduino program to read some data, displaying it through the Serial port.

The circuit

Wiring for simple analog circuit to read value of a potentiometer
Closeup of wiring the potentiometer

We wire a Potentiometer so that one end is connected to the +5 volt pin, the other end is connected to the Ground pin, and the middle pin (the wiper) is connected to analog pin A0.

There's a huge range of possible sensors and such that can be connected to an Arduino. This is one of the simplest, but as we go through the following Sketch imagine other possible sensors.

The wiring for this is easily accomplished on a simple prototyping breadboard. It was fortunate the surface-mount potentiometer I happened to have fit with the holes on the breadboard. Wired as so, a simple screwdriver is enough to adjust the potentiometer, and pin A0 will receive a variable voltage depending on where the potentiometer is turned.

The Sketch

Now let's look at how we read the analog data, and print to the serial console.

int sensorPin = A0;  

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}

The setup function simply initializes the Serial connection. We've set it to 9600 baud, so set the Console to the same data rate.

In loop we use analogRead to read pin A0 giving us an int from 0 to 1023. We print the value using Serial which goes out to the serial console as previously configured.

A call to delay keeps the data being printed at a reasonable rate.

You may want to convert the 0..1023 value into a voltage. We know from the circuit the value represents a voltage between 0 to 5 volts. Some division and conversions are required, which is an exercise for the reader.

A common application is to have multiple sensors, and a line-by-line output of data, with an easily generated and easily parsed format. For example a timestamp followed by several numerical values all separated by a space or tab character. The serial line outputting this data could be connected to another device that records the outputted data. There might even be commands sent to the device to cause relays to open or close. Arduino's are capable of this sort of thing.

Reference

« US Postal Service introduces Informed Delivery, a Big-Brotherly preview of incoming postal mail Correct character encoding with DOMDocument implementing a Wordpress content filter »
2016 Election Acer C720 Ad block AkashaCMS Amazon Amazon Kindle Amazon Web Services America Amiga and Jon Pertwee Android Anti-Fascism AntiVirus Software Apple Apple Hardware History Apple iPhone Apple iPhone Hardware April 1st Arduino ARM Compilation Artificial Intelligence Astronomy Astrophotography Asynchronous Programming Authoritarianism Automated Social Posting AWS DynamoDB AWS Lambda Ayo.JS Bells Law Big Brother Big Finish Bitcoin Mining Black Holes Blade Runner Blockchain Blogger Blogging Books Botnets Cassette Tapes Cellphones China China Manufacturing Christopher Eccleston Chrome Chrome Apps Chromebook Chromebox ChromeOS CIA CitiCards Citizen Journalism Civil Liberties Clinton Cluster Computing Command Line Tools Comment Systems Computer Accessories Computer Hardware Computer Repair Computers Cross Compilation Crouton Cryptocurrency Curiosity Rover Currencies Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Developers Editors Digital Photography Diskless Booting Disqus DIY DIY Repair DNP3 Do it yourself Docker Docker MAMP Docker Swarm Doctor Who Doctor Who Paradox Doctor Who Review Drobo Drupal Drupal Themes DVD E-Books E-Readers Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Encabulators Energy Efficiency Enterprise Node EPUB ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Face Recognition Facebook Fake News Fedora VirtualBox File transfer without iTunes FireFly Flickr Fraud Freedom of Speech Front-end Development Gallifrey git Github GitKraken Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Heat Loss Hibernate Hoax Science Home Automation HTTP Security HTTPS Human ID I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick In-memory Computing InfluxDB Infrared Thermometers Insulation Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man iTunes Java JavaScript JavaScript Injection JDBC John Simms Journalism Joyent Kaspersky Labs Kindle Kindle Marketplace Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac Mini Mac OS Mac OS X Machine Learning Machine Readable ID macOS MacOS X setup Make Money Online March For Our Lives MariaDB Mars Mass Violence Matt Lucas MEADS Anti-Missile Mercurial MERN Stack Michele Gomez Micro Apartments Microsoft Military AI Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications Mobile Computers MODBUS Mondas Monetary System MongoDB Mongoose Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Network Attached Storage Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea npm NVIDIA NY Times Online advertising Online Community Online Fraud Online Journalism Online Photography Online Video Open Media Vault Open Source Open Source Governance Open Source Licenses Open Source Software OpenAPI OpenVPN Palmtop PDA Patrick Troughton Paywalls Personal Flight Peter Capaldi Phishing Photography PHP Plex Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero ReactJS Recaptcha Recycling Refurbished Computers Remote Desktop Removable Storage Republicans Retro Computing Retro-Technology Reviews RFID Right to Repair River Song Robotics Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Russian Hacking Rust SCADA Scheme Science Fiction SD Cards Search Engine Ranking Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Serverless Framework Servers Shell Scripts Silence Simsimi Skype SmugMug Social Media Social Media Warfare Social Network Management Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spear Phishing Spring Spring Boot Spy Satellites SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Stand For Truth Strange Parts Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Tom Baker Torchwood Total Information Awareness Trump Trump Administration Trump Campaign Twitter Ubuntu Udemy UDOO US Department of Defense Virtual Private Networks VirtualBox VLC VNC VOIP Vue.js Web Applications Web Developer Resources Web Development Web Development Tools Web Marketing Webpack Website Advertising Weeping Angels WhatsApp William Hartnell Window Insulation Windows Windows Alternatives Wordpress World Wide Web Yahoo YouTube YouTube Monetization