Switch interfacing with Beaglebone black
This tutorial explains how to interface switch with Beaglebone black where switch acts as an input device. It is a simple learning tutorial regarding the use of GPIO pin as an input. Program is written in python script with adafruit GPIO library. It is a not a big deal but it’s necessary to clear fundamental before developing high application.
- Beaglebone Black
- Push button
- 330 Ω Resistor
- 1 kΩ Resistor
- Female to Female connectors
Setup of Software environment
Install the latest python version in BBB as explained in tutorial How to make first python program with Beaglebone Black. Install the adafruit python-GPIO library named adafruit_BBIO.
It is a simple learning tutorial of Beaglebone black. Here I connected led and switch with GPIO pin of Beaglebone black. When script is being executed, it enters the end of continuous loop. When switch is pressed, led is an ON otherwise it is OFF. Press ctrl+C to stop the execution of program form SSH command terminal.
Let’s first prepare the circuit connection. Take a breadboard and provide VCC and ground from BBB to breadboard line. Connect Supply 3.3 V from pin number 3rd of header P9 and ground from pin number 2nd of header P8. Connect the negative terminal of LED with ground. Connect the 330 ohm resistor with positive terminal of LED. Connect another end of 330 ohm resistor with pin number 8 of header P8. Push button has two terminals. Any one of them connects with ground. Provide the VCC 3.3 V through 1k ohm resistor to another terminal of push button. Common end of resistor and push button, connect with pin number 9th of header P8. Provide the supply to Beaglebone black by connecting with PC through USB cable. Now your circuit is prepared.
Open the command terminal and take an access of Beaglebone black through SSH as explained in getting started with Beaglebone black. Create a new file using touch command with .py extension (i.e. led.py). Open the file with any text editor (i.e. nano, vim etc.) and write a code in python language.
Configuration of GPIO pin
Import the GPIO library from adafruit Beaglebone black library by calling following line in program:
import Adafruit_BBIO.GPIO as GPIO
You can define the pin number as a pin number with underscore followed by pin number. (i.e. P8_8). Give an appropriate name to the pin number.
i.e. BUTTON = “P8_9”
Here, I assigned the name BUTTON to pin number 9th oh header P8.
Next, configure the pin as input or output as following function:
GPIO.setup (Pin number, output/input)
For example, I declared button (pin number 9th oh header P8) as an input by following line:
Note: here I declared switch as an input. (led is input device)
You can read the status of input pin by following function:
GPIO.input (pin number)
You can make pin high and low by following function:
GPIO.output (Pin number, High or Low)
For example, I make LED (pin number 9th oh header P8) as High and low by following line:
HIGH : GPIO.output (LED, GPIO.HIGH)
LOW : GPIO.output (LED, GPIO.LOW)
Run the script from terminal:
Enter the following command with file name from command prompt:
i.e. python switch.py
Filed Under: Tutorials