Getting Started with the BrickPi

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: Shawn Hymel
Favorited Favorite 0

Programming

Open a terminal (if you do not already have one open) and create a new file for our motor and sensor test. Open that file with Leadpad (Raspbian's default text editor).

leafpad motor_sensor.py

Enter the following code into the text editor:

language:python
from BrickPi import *

BrickPiSetup()

BrickPi.SensorType[PORT_1] = TYPE_SENSOR_TOUCH
BrickPi.MotorEnable[PORT_A] = 1

BrickPiSetupSensors()

while True:
        result = BrickPiUpdateValues()
        if not result:
                if BrickPi.Sensor[PORT_1]:
                        BrickPi.MotorSpeed[PORT_A] = 200
                else:
                        BrickPi.MotorSpeed[PORT_A] = 0
        time.sleep(0.01)

Raspberry Pi Leafpad with BrickPi Python code

Save and exit out of Leafpad. In the console, enter the following:

python motor_sensor.py

Now, whenever you press the Touch Sensor, the Servo Motor should move!

BrickPi Python example working

Press ctrl+c to end the program.