In this article we look at an AD keypad connected to a Beaglebone first of all lets look at the sensor.
I purchased this module as part of a microcontroller kit, its an interesting little module as it has 16 buttons but with only 1 I/O line is required, an analog input. You connect this to your Beaglebone and read in the value.
Now the Beaglebone only supports 1.8v input as a maximum on the ADC pins, this is not a problem in this case as we are using the 1.8v reference from VDD_ADC on P9_32 and we will use one of the AIN pins.
You can see this in the image below on the P9 header
The concept is straightforward you use a resistor network as voltage dividers, and then let each button feed a different voltage to the analog pin. So by detecting the voltage you can tell which button has been pressed. You can only detect one button at a time
This example needs the Adafruit IO python library installed – https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black
import Adafruit_BBIO.ADC as ADC import time sensor_pin = 'P9_40' ADC.setup() print('Reading\t\tVolts') while True: reading = ADC.read(sensor_pin) volts = reading * 1.800 print('%f\t%f' % (reading, volts)) time.sleep(1)
Save this as adkey.py, I used the Cloud 9 IDE
Now for a gotcha, if you click on the Run button you will see something like this
Traceback (most recent call last):
File “/var/lib/cloud9/iain/adkey.py”, line 6, in <module>
RuntimeError: Unable to setup ADC system. Possible causes are:
– A cape with a conflicting pin mapping is loaded
– A device tree object is loaded that uses the same name for a fragment: helper
You need to run the python script using administrator privileges, open a terminal and enter the following
sudo python adkey.py
After running you should see something like this.
debian@beaglebone:/var/lib/cloud9/iain$ sudo python adkey.py
The first bolded set of numbers was the K0 button pressed, the K1 button was the second set of bolded numbers. The 0.000488 is with no buttons pressed. In your code you would need to check for a range so 0.999 to 0.9799 could be K0.