RobotGeek 101: Analog Input Introduction

This lesson is part of the RobotGeek 101 Learning Series. If you haven't already, please download the example sketches, and follow through the Geekduino Getting Started Guide to handle the foundations of using the Arduino IDE in conjunction with the Geekduino.

Welcome to RobotGeek 101 : Analog Input. This lesson will show you how to use the Geekduino's Analog Input pins to read signals from Analog Sensors like knobs and sliders. The following video for this lesson will walk you through everything you need to do to get started with Analog Input.

Parts used in this lesson:

These parts and everything you need to complete chapters 1-7 in RobotGeek 101 are available in the RobotGeek Basic Experimenter's Kit.

Contents:

Introduction

potvolta.jpg

So what's the deal with Analog Input? We've covered Digital Input with the metaphor of hearing, but Digital only has two states - on and off. If we continue with that description, the pin expecting Digital Input can only "hear" if a signal is off (5V) or when it's at full volume (5V on the Geekduino). On the other hand, and Analog Input Pin can hear the full spectrum of volume (anywhere between 0-5V on Geekduino and most Arduino boards). Analog Input pins send a voltage to the microprocessor which measures the voltage and the interprets the signal.

Analog Inputs are a great way to take control of your robots - you can joystick to tell the robot which way to move, or use a knob to change the intensity of an LED. Analog Inputs are versatile, and will be a vital part of your projects. We're going to start off with an example that gives you real time feedback for the physical movement of an Analog Input Device.

Back to Contents

A-analogInputSerialOutput

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's use the Arduino IDE to read an analog input and print out the data to the serial port. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 03 - analogInput > A-analogInputSerialOutput

We set up a constant integer to store the pin that the potentiometer is connected to. It is a 'const int' because it will not change during code.

const int ANALOG_PIN = 0;

Then we set up an integer that will change with the current position of the potentiometer.

int analogValue; 

Now we're going to set up a float variable called analogVoltage. A float is a variable that has a decimal point. This uses more memory, but it is more precise, which is what we need to calculate the voltage from the potentiometer.

float analogVoltage;

We start the serial port at the default baud rate of 9600 in the setup, and move on to the loop. In the loop, we store the analogValue variable from the the analog pin with the analogRead function.

analogValue = analogRead(ANALOG_PIN);

Next, to get our voltage reading, we use a bit of math to turn the analog value (0-1023) into its approximate voltage value (0-5v).

Analog Value Voltage
0 0V
512 2.5V
1023 5V
analogVoltage = 5.0 * (analogValue/1023.0);

Lastly, we print these values over serial with a short delay. Running this code and playing with the knob can help you intuitively understand what values will give you the results you are expecting.

Back to Contents

B-analogBlinkControlDelay

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's use the Arduino IDE to blink an LED on and off at a rate determined by an analog input such as a knob. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 03 - analogInput > B-analogBlinkControlDelay

This time, we're using integers for the analog value and delay time, while using a boolean for the led state.

int analogValue;
bool ledState;
int delayTime;

Just like example A, we're storing the analog value to a variable, but this time, we're using it to set the delay time. The analog value is a number in the range of 0-1023. If we used it directly for the delay time, we would have between 0 and 1023 milliseconds of delay time, which is mostly too fast for us to see the results. We do a little bit of math, multiplying the analog value by 5 and adding 100, so the delay time range is between 500-10730 milliseconds, or between half a second and a little over 10 and a half seconds.

delayTime = (analogValue * 5) + 100;

Here's where the use of the boolean comes in handy. Since the boolean has only two states, we can easily invert the state using the following line of code

ledState = !ledState;

We continue by writing the altered state to the LED pin, and delaying according to the delay time as set by the analog value.

  digitalWrite(LED_PIN, ledState);
  delay(delayTime);

Back to Contents

C-analogBlinkControlWithoutDelay

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's use the Arduino IDE to blink an LED on and off at a rate determined by an analog input such as a knob. Using non-blocking code, this sketch is more responsive than code that uses delay(). Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 03 - analogInput > C-analogBlinkControlWithoutDelay

This one looks very similar to the last one, but what's this 'unsigned long' business about? Unsigned long variables store 32 bit numbers, meaning they can store any number between 0 and 4,294,967,295! This will be useful later.

unsigned long lastBlinkTime;

The delay function is great for its simplicity, but it has the drawback of having to go through the entire time set in the delay before going through the loop again. This comes across as a lack of responsiveness when it comes to actual usage, and can gum up the works on larger projects. Instead of using the code to use the knob position to change the delay of all functions after changing the state of the LED, we're going to tell it to use the knob position to determine how quickly to invert the LED state.

  if(millis() - lastBlinkTime > delayTime)
  {
    lastBlinkTime = millis();
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
  }

Back to Contents

Analog Input Reference

Now that you've completed the RG101 Analog Input Introduction, you're ready to go onto the next topic : 6.1 PWM .

If you are interested in learning more about Analog Input, check out our Analog Input Reference Lesson. In this lesson we will look at the remaining RG101 examples and provide you with links to advanced Analog Input topics.

Back to Contents

Feedback Form


Search