Skip to main content

Analog Input with a Potentiometer

This example will guide you through the process of connecting a potentiometer to your imp, and polling its value at a specified interval.

This example assumes you have a basic familiarity with Electric Imp’s impCentral™. We suggest working through our Getting Started Guide before trying this example.

What You Will Need

Note The following instructions were written with the imp001 in mind, but they can be readily adapted to any other imp and breakout board.

For this example you are going to need the following:

  • 1 x April breakout board
  • 1 x imp001 card
  • 1 x breadboard
  • 1 x potentiometer
  • 3 x jumper wire

Circuit

Analog input with potentiometer

Hook Up Instructions

  1. Connect the middle lead of the potentiometer to pin2 on the imp.
  2. Connect one of the outside leads of the potentiometer to the 3v3 pin on the imp.
  3. Connect the other outside lead of the potentiometer to the GND pin on the imp.

Code

In impCentral, create a new Product called “Examples” (if you have not tried another of these basic hardware examples) and a new Development Device Group called “potentiometer”. Assign your device to the new Device Group, then copy and paste the following code into the code editor’s ‘Device Code’ pane and hit ‘Build and Force Restart’.

What’s Going On

The first thing we do is assign hardware.pin2 to a global variable called pot, this will make it easier to reference throughout the rest of our code:

pot <- hardware.pin2;

Next, we configure pot to be an analog input:

pot.configure(ANALOG_IN);

We can now use pin.read() to get the current voltage of the pin. The result of a pin.read() on an ANALOG_IN pin will be an integer value between 0 and 65,535 (212).

In our example, we create a poll function, poll(), that will read and log the value of the pin ten times a second. It reads pin2 and logs the value:

server.log(pot.read());

and then schedules itself to run again in 0.1 seconds using imp.wakeup():

imp.wakeup(0.1, poll);

Finally, we start polling the pin by calling the poll() function to get the loop going:

poll();