Introduction

In this lesson we’ll look at how to read the values coming from a potentiometer on the Beaglebone Black using its analog pins. A potentiometer is a variable resistor whose value can be modified by turning the knob or screw located on it. This will allow more or less current to flow, thus varying the voltage.

How do analog pins work?

 

The Beaglebone Black has 7 analog pins that can be used to retrieve values from sensors, or to control analog components. The pins are connected to an analog-to-digital converter (8 bits) to make the values readable by the Beaglebone Black.

Analog pins operate at +1.8V maximum! So you can’t use components requiring +5V or +3.3V. In the rest of the course, we’ll connect the potentiometer to the ADC’s VDD and GND in order to obtain the correct voltage.

Electronic diagram

Here is the program that allows me to read the voltage values coming from the potentiometer and convert them into percentages:

Program

 

The following program allows you to retrieve analog values from the ADC, in particular the pin where our potentiometer is located. To do this, you’ll need to install the Adafruit_BBIO library.

You can install it with the following command:

sudo pip3 install Adafruit_BBIO
import Adafruit_BBIO.ADC as ADC
import time

# ADC channel configuration
potentiometer_pin = "P9_36" # You can adjust the pin number according to your configuration

ADC.setup()

try:
    while True:
        # Reading the analog value of the potentiometer
        pot_value = ADC.read(potentiometer_pin)

        # Convert the value to a more meaningful range (e.g. 0 to 100)
        scaled_value = int(pot_value * 100)

        # Show value
        print("Potentiometer value: {}".format(scaled_value))

        # Wait a short time before the next reading
        time.sleep(0.1)

except KeyboardInterrupt:
    # Stop the program properly in case of interruption (Ctrl+C)
    pass

finally:
    # Clean ADC resources
    ADC.cleanup()