BrickPi Python in IDLE

BrickPi Python in IDLE

Run the BrickPi on Python IDLE

Python IDLE (integrated development evironment) is often the preferred environment for education and learning Python.  Below we have tried to outline the steps for using IDLE Python with the BrickPi.

IDLE and the LEGO MINDSTORMS on Raspberry Pi

Before beginning:

  1. You must use an image that has been prepared for use with the BrickPi. See here.
  2. Click on the IDLE icon on the Raspberry Pi.  

Note that Python in IDLE is case sensitive with variables.  For example, when you use the variable PORT_A, you must type it “PORT_A” (not Port_A or port_a; these spellings will cause an error).

Example: Run a motor.

Enter the following into IDLE:

from BrickPi import *
BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPiSetupSensors()
BrickPi.MotorSpeed[PORT_A] = 200
BrickPiUpdateValues()

Motor_Example_In_Raspberry_Pi_IDLE

Example: Run a motor back and forth.

Enter the following into IDLE:

from BrickPi import *
BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPiSetupSensors()
def run_motors():
 while True:
  BrickPi.MotorSpeed[PORT_A] = 200
  BrickPiUpdateValues()
  time.sleep(2)
  BrickPi.MotorSpeed[PORT_A] = -150
  BrickPiUpdateValues()
  time.sleep(5)
run_motors()

Example: Run one motor forward and one backward.

Enter the following into IDLE:

from BrickPi import *
BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.MotorEnable[PORT_D] = 1
BrickPiSetupSensors()
def run_motors():
 while True:
  BrickPi.MotorSpeed[PORT_A] = 200
  BrickPi.MotorSpeed[PORT_D] = -100
  BrickPiUpdateValues()
  time.sleep(2)   # Pause for 2 seconds.
  BrickPi.MotorSpeed[PORT_A] = -150
  BrickPi.MotorSpeed[PORT_D] = 200
  BrickPiUpdateValues()
  time.sleep(5)
run_motors()

Setup a touch sensor.

Enter the following into IDLE:

from BrickPi import *
BrickPiSetup()
BrickPi.SensorType[PORT_1] = TYPE_SENSOR_TOUCH
BrickPiSetupSensors()
def run_sensor():
 while True:
 result = BrickPiUpdateValues()
 if not result:
 print BrickPi.Sensor[PORT_1]
 time.sleep(0.01)
run_sensor()

Have a question? Ask on our forums!

{"cart_token":"","hash":"","cart_data":""}