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

Arduino Tutorial 04: analogRead Serial Monitor with Potentiometer - YouTube

Source: (www.youtube.com) Mert Arduino and Tech

Arduino Basic Tutorial 03 - analogRead Serial Monitor with Potentiometer Recommended site to buy the required hardware: https://www.banggood.com/?p=CS1204785...

Tutorial 09: How to read voltages with analogRead(): Arduino Course for Absolute Beginners (ReM) - YouTube

Source: (www.youtube.com) Programming Electronics Academy

*Click Below to Sign up for the free Arduino Video Course:* https://opensourcehardwaregroup.com/thearduinocourse/ *Click Below to Check Out the Premium Ardui...
« 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 Amiga Android Anti-Fascism Apple Apple Hardware History Apple iPhone Hardware April 1st Arduino ARM Compilation Astronomy Asynchronous Programming Authoritarianism Automated Social Posting Bells Law Big Brother Black Holes Blade Runner Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Civil Liberties Clinton Cluster Computing Command Line Tools Computer Hardware Computer Repair Computers Cross Compilation Crouton Curiosity Rover Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Digital Photography DIY DIY Repair Docker Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Enterprise Node ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Home Automation HTTPS I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick InfluxDB Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Joyent Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X Make Money Online MariaDB Mars Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications MODBUS Mondas Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea Online advertising Online Fraud Open Media Vault Open Source Governance Open Source Software OpenAPI OpenVPN Personal Flight Peter Capaldi Photography PHP Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Remote Desktop Republicans Retro-Technology Reviews Right to Repair River Song Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Scheme Science Fiction Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spring Spring Boot SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Ubuntu UDOO Virtual Private Networks VirtualBox VLC VNC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Weeping Angels WhatsApp Wordpress YouTube