Introducing CodeBug! |
This project will actually be a little different than our previous projects. Rather than taking advantage of the Blockly interface and emulation plug in on
http://www.codebug.co.uk. we'll be bringing the Raspberry Pi into the mix.
Follow the steps below to get started on this advanced project Thomas Macpherson-Pope pulled together from CodeBug.org.uk
What you need:
Computer
The Project
Connecting CodeBug to your Raspberry Pi can unleash it’s full potential. Your programs can:
- Make CodeBug respond or control Minecraft
- Display your Tweets on CodeBug
- Control CodeBug from hundreds of miles away remotely over the web
- Turn CodeBug’s LEDs on and respond to its buttons
- Create games, and anything you can imagine, and program in Python
- Hook up with Internet of Things devices
CodeBug tethering works by putting a special project file onto your bug and plugging CodeBug onto your Raspberry Pi’s GPIO pins. To learn how to put the special project file for teather on to the CodeBug please see 10 CodeBug Projects in 10 Days: Teathering CodeBug with Python.
Next we will need to fit the CodeBug onto the Raspberry Pi. CodeBug connects straight to Raspberry Pi's GPIO header through CodeBug''s expansion connector. While the Pi is diconnected from power, align CodeBug to the pins shown in the diagrams below and gently push CodeBug's connector onto the GPIO pins. Make sure to NEVER connect the Micro USB or use a battery with CodeBug while it is fitted to a Raspberry Pi.
Make sure you select the appropriate Raspberry Pi model, the Raspberry Pi expanded its GPIO header from 26 pins to 40 with the Raspberry Pi Model B+
If you are still unsure which Raspberry Pi GPIO pins to connect CodeBug to, note the pin labels on the back of CodeBug, and connect these to the corresponding pins on your Raspberry Pi.
Now it's time to power up the Raspberry Pi !
First, let's make sure to enable I2C by opening a Terminal window and running
sudo raspi-config
Next let's choose:
Advanced Options > Would you like the I2C interface to be enabled? > Yes Would you like the I2C kernel module to be loaded by default? > Yes
Next we will install the Python libraries that will talk to the CodeBug using the I2C GPIO pins.
sudo apt-get update sudo apt-get install python3-codebug-i2c-tether
Download this example to your Raspberry Pi (right click Save Link As…)
We'll run the example in the Terminal with the following command
python3 example.py
We should see an arrow pointing up-left on the CodeBug’s LED display!
We can write our own Tethered CodeBug programs using Python and a few simple commands to control CodeBug. In the next steps we will start an interactive Python session and enter commands to interact with the tethered CodeBug.
Open a Terminal and type
python3
There will be a python prompt appear >>>. Let's import the CodeBug I2C library by entering
import codebug_i2c_tether cb = codebug_i2c_tether.CodeBug() cb.open() cb.set_pixel(2,2,1)
We should see the center LED light up on CodeBug.
Next we'll try setting a row of CodeBug’s LEDs at the same time
cb.set_row(3,0b10100)
We should see the third row of LEDs light up in the sequence we gave. 0b10101 is a binary value. The 0b shows that it is binary and the 10100 determines which LEDs are on. 1 represents on and 0 represents off. This means that the LED to the far left is on (column 0), the next LED is off and the next on (column 2), and the other two LEDs are off.
Now its time to write text on the CodeBug’s LEDs, using the command
cb.write_text(0, 0,'A')
An A will appear on the CodeBug LEDs
Write scrolling text on the CodeBug’s LEDs, using the command
for i in range(0,-30,-1): cb.write_text(i, 0, 'Hello', direction="right") time.sleep(.1)
The text "Hello" will scroll
Check whether a button is pressed, by giving get_input either an ‘A’ or a ‘B’ or numbers 0-3.
cb.get_input('A')
This will return True if the button is pressed, otherwise it will return False.
To access the full list of command available we can type the following on the interactive Python shell with the codebug_i2c_tether library imported.
help(codebug_i2c_tether.CodeBug)
We can write long programs for Tethered mode CodeBug by writing commands in the text editor and saving and running the file in the way we did with the examples earlier, (python yourfile.py).
Tethered mode gives CodeBug access to the full computing power, functionality and network connectivity of the Raspberry Pi!
Make the most of the variety of powerful yet easy to use Python modules allow the CodeBug to generate random numbers, create email alerts or even post to Twitter!
See more CodeBug projects and learn how you can get one of your own by visiting: 10 CodeBug Projects in 10 Days
Top Comments