Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more for just $11.99/month.

analogRead() and the Serial Port

analogRead() and the Serial Port

FromLearn Programming and Electronics with Arduino


analogRead() and the Serial Port

FromLearn Programming and Electronics with Arduino

ratings:
Length:
12 minutes
Released:
Mar 11, 2017
Format:
Podcast episode

Description

Knowing if something is on or off can be extremely useful, but often you will want to know more. How bright is the light? How fast is the satellite moving? These types of answers are often analog – they cover a large range of values, not just on or off. The Arduino handles analog inputs with 6 dedicated pins, labeled A0 through A5. These pins have access to an analog-to-digital converter, which takes the range of input values and creates a digital version by cutting up the range into tiny pieces. All this is handled behind the scenes – all you have to do is use some very simple functions and you will get what you need. You Will Need Potentiometer (any resistance range will work) Jumper Wires – at least 3 Bicycle tire Step-by-Step Instructions Place the potentiometer into your breadboard. Run a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of your potentiometer. Run another jumper wire from one of the ground pins on your Arduino (labeled GND) to the other outside pin of the potentiometer. Run the final jumper wire from pin A0 on your Arduino to the middle pin of the potentiometer. Plug the Arduino into your computer. Open up the Arduino IDE. Open the sketch for this section. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished. On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen. Now adjust the knob of the potentiometer and watch the serial monitor window. The numbers should adjust between 0 and 1023. Using the Arduino analogread and map function with a potentiometer at pin A0 This image composed with Fritzing. The Arduino Code /* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(1); // delay in between reads for stability } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(1); // delay in between reads for stability } Discuss the Sketch This sketch starts with a multi-line comment describing the sketch and the circuit. You will probably notice that the first block of code is the setup() function – we do not declare or initialize any variables at the beginning of this sketch – instead we will do this inside the loop() function, as in the last example. Inside the curly braces of setup() we revisit the Serial library and use the function Serial.begin(). void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } 1 2 3 4 5 6 7 void setup() { // initialize serial communication at 9600 bits per second: Se
Released:
Mar 11, 2017
Format:
Podcast episode

Titles in the series (61)

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino. We'll include some lessons from the first edition and the second edition of our training course.