Raspberry Pi Controlled Codebug With I2C

Introduction

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.

You will need
CodeBug CodeBug
Micro USB cable
Raspberry Pi

  • Ask a new question:

Set up Tethered mode

For Tethered mode you need to load your CodeBug with a special project file. This is downloaded to CodeBug just the same as a regular user program (refer to the download guide for details).

Download this I2C Tether mode project and load it onto your CodeBug.


5 Questions
  • is it possible to connect multiple codebugs in a chain with i2c ? Martin Lilliendal Hansen @ Dec. 15, 2015, 1:11 p.m.


    Not currently, but you can connect multiple CodeBugs to a single computer over USB and communicate between them through that Thomas Macpherson-Pope @ Dec. 16, 2015, 10:18 a.m.

    ok thanks :) Martin Lilliendal Hansen @ Dec. 16, 2015, 10:56 a.m.

    ok thanks :) Martin Lilliendal Hansen @ Dec. 16, 2015, 10:56 a.m.

    will i be able to chain them together with input/output ? starting the chain with a button and then passing it forward -is that possible ? pseudo code : void loop{ if(input 0 == true){ output 1 = true; //do some ligth }else{ output 1 = false; // do some other light } } Martin Lilliendal Hansen @ Dec. 16, 2015, 11:05 a.m.
    • Yes this is possible. https://twitter.com/MrMcKune/status/708642831993344000 — Thomas Macpherson-Pope @ March 24, 2016, 10:18 a.m.


    Answer this question:

  • When I press the button B it ejects the CodeBug. Why? ehsanfshah @ Nov. 19, 2015, 1:10 p.m.


    Pressing button B resets the CodeBug to take it out of programming mode and into the user's project code, and so ejects the CodeBug drive from the computer. Thomas Macpherson-Pope @ Nov. 19, 2015, 1:16 p.m.

    I don't understand that. I have to copy this tether file to the codebug. But when I want to connect the Codebug to i2c I have to remove the battery - then the thether file is deleted?! What do I wrong? I cant run the python file. I always got errors root@raspberrypi:~/Downloads# python3 example.py Traceback (most recent call last): File "example.py", line 7, in <module> codebug.set_row(4, 0b11100) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/core.py", line 90, in set_row self.set(row, val) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/codebug_i2c.py", line 55, in set value)) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/i2c.py", line 85, in transaction ioctl(self.fd, I2C_RDWR, ioctl_arg) OSError: [Errno 5] Input/output error Thanks NeoCortex @ Dec. 21, 2015, 1:01 p.m.

    Answer this question:

  • Okay. Thanks. I followed your video online Getting started with CodeBug: https://www.youtube.com/watch?v=Si-j4i4Cpr4 The problem is still there; After creating the message, I downloaded the file successfully and dragged and dropped into the CodeBug. The CodeBug receives the file successfully. When I press the button B, it runs the message but also ejects it... Just to let you I am using MacBook Pro. ehsanfshah @ Nov. 19, 2015, 4:58 p.m.


    Yep this is correct behaviour. You can now disconnect your CodeBug if you wish and insert a battery to let CodeBug run the message Thomas Macpherson-Pope @ Nov. 19, 2015, 5:11 p.m.
    • Thanks. I did what you suggested, and it worked but how to keep it running... Here is the Code: codebug_direction('U'); io_configure(0, 1); io_configure(1, 1); io_configure(2, 1); io_configure(3, 1); io_configure_pullup(0, 0); io_configure_pullup(2, 0); io_configure_pullup(3, 0); fivebyfivedisplay.sprite_scroll((new StringSprite('EHSAN', 'R')), 200, 'L'); Thanks — ehsanfshah @ Nov. 20, 2015, 5:51 p.m.

    • Such a silly question to ask! I put a repeat loop in... It is working... Thanks — ehsanfshah @ Nov. 20, 2015, 6:55 p.m.

    • Glad to hear you're all sorted :) enjoy — Thomas Macpherson-Pope @ Nov. 23, 2015, 11:17 a.m.


    Þorvaldur Tumi @ Dec. 25, 2015, 5:31 p.m.

    Answer this question:

  • I don't understand that. I have to copy this tether file to the codebug. But when I want to connect the Codebug to i2c I have to remove the battery - then the thether file is deleted?! What do I wrong? I cant run the python file. I always got errors root@raspberrypi:~/Downloads# python3 example.py Traceback (most recent call last): File "example.py", line 7, in <module> codebug.set_row(4, 0b11100) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/core.py", line 90, in set_row self.set(row, val) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/codebug_i2c.py", line 55, in set value)) File "/usr/lib/python3/dist-packages/codebug_i2c_tether/i2c.py", line 85, in transaction ioctl(self.fd, I2C_RDWR, ioctl_arg) OSError: [Errno 5] Input/output error NeoCortex @ Dec. 22, 2015, 5:43 a.m.

    • I have the same problem! :( — Emil Qvarnström @ Aug. 24, 2016, 4:06 p.m.


    Answer this question:

  • How do you write code that accesses the I2C hardware? I did not seed it on the programming interface online and someone had to have done this in order to make this project work for the Raspberry Pi I2C tether interface. I would like to do my own I2C stuff for more connectivity. thanks Clay Barclay @ March 9, 2016, 4:48 p.m.


    You can not currently access the I2C directly from the block interface. We created the tethered mode project, but not using Blockly blocks. Thomas Macpherson-Pope @ March 14, 2016, 5:33 p.m.
    • Any planed extensions to the Blocky blocks for this or other cool abilities? thanks — Clay Barclay @ March 14, 2016, 5:43 p.m.

    • we are continually developing new features and ways to use codebug — Thomas Macpherson-Pope @ March 24, 2016, 10:21 a.m.


    Answer this question:

  • Ask a new question:

Fitting CodeBug to Raspberry Pi

CodeBug connects straight to Raspberry Pi’s GPIO header through CodeBug’s expansion connector. While your Pi is disconnected from power, align CodeBug to the pins shown in the diagrams below and gently push CodeBug’s connector onto the GPIO pins.

Choose your matching Raspberry Pi model

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.

You can now power up your Raspberry Pi.


3 Questions
  • so how would the codebug get power? joseph b @ Oct. 27, 2015, 8:17 a.m.

    • Through the RPi — Jenny @ Oct. 27, 2015, 9:57 a.m.

    • The pi gives it 3.3v onthe Vcc pin — Mike Redrobe @ Oct. 30, 2015, 3:39 p.m.


    CodeBug will be powered by the VCC (3v3) Raspberry Pi GPIO pin Thomas Macpherson-Pope @ Oct. 27, 2015, 10:28 a.m.

    Answer this question:

  • GREAT WORK GUYS, JUST BURN MY RPI2..... YOU SHOULD PUT THAT WARNING IN BIGGER BLINKING RED FONT. GUESS SOME PROTECTIVE CIRCUIT COULD BE APPLIED HERE, THERE SHOULD BE MORE VICTIM.... ESPECIALLY IF YOU EXPECT KID TO PLAY WITH THIS. devsar @ Aug. 17, 2015, 5:09 a.m.


    Thanks for the feedback devsar, we have made the warning more eye-catching so people don't skip it. Thomas Macpherson-Pope @ Aug. 17, 2015, 3:36 p.m.
    • why would raspi be damaged if the battery is only 3V when the battery is fitted? — victagayun @ Nov. 7, 2015, 1:59 p.m.


    Answer this question:

  • How is it possible to damage Raspi if the batt is only 3V and raspi IO is 3.3v? — victagayun @ Nov. 14, 2015, 11:35 a.m.

    • This is not only about voltage. For example if the +3V of the CodeBug (Battery-Powered) is connected to the GND (0V) of the Raspery and the other way around, Damaging the Pi, the Bug or both is still possible. It also depends on the whirering. — Sadi Yigit @ Jan. 23, 2016, 1:40 p.m.


    The Pi would be damaged if 5V from the USB is connected. The battery should be removed as they will try to power the Pi at 3V3 when the Pi's power source is removed. Do not have the battery or USB connected when connecting CodeBug to the Raspberry Pi. Thomas Macpherson-Pope @ Nov. 16, 2015, 10:48 a.m.
    • yes I understand the 5V might overcharge the batt of 3.3v and it might explode. My question is, having the battery inserted and connect to RPi is the one I cant understand. 3.3V might overcharge and the battery might explode, but the RPi should still be ok, right? — victagayun @ Nov. 16, 2015, 10:58 a.m.

    • yes I understand the 5V might overcharge the batt of 3.3v and it might explode and might damage the RPi. My question is, having the battery inserted and connect to RPi is the one I cant understand. 3.3V might overcharge and the battery might explode, but the RPi should still be ok, right? — victagayun @ Nov. 16, 2015, 10:59 a.m.

    • There are two separate issues. The Pi would be damaged by the 5V from the USB going into the 3V3 on the GPIO. The battery would power the pi when it's power source is removed, and at 3V3 the Pi may not function as expected. — Thomas Macpherson-Pope @ Nov. 16, 2015, 11:05 a.m.


    Answer this question:

  • Ask a new question:

Enable Raspberry Pi I2C

First, make sure you have enabled I2C by opening a Terminal window and running

sudo raspi-config

And then choose

Advanced Options > I2C then select Yes for both questions.


1 Question
  • It was not enough for proper connection. I followed this https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c from section: Installing Kernel Support (Manually) ... and works fine. Andras Low @ Aug. 7, 2015, 12:29 p.m.

    • try connecting to a wire — joseph b @ Oct. 28, 2015, 8:39 a.m.

    • yeah!!! Should add it manually, but the rpi config should do that... — victagayun @ Nov. 7, 2015, 2:45 a.m.

    • you can also try this tutorial: http://ozzmaker.com/i2c/ They should add it here! — victagayun @ Nov. 14, 2015, 11:45 a.m.



    Answer this question:

  • Ask a new question:

Python libraries for Raspberry Pi

You must now install the Python libraries that will talk to your CodeBug using the I2C GPIO pins.

In your Terminal window, type

sudo apt-get update
sudo apt-get install python3-codebug-i2c-tether

4 Questions
  • for me the second line is: sudo pip-3.2 install codebug_i2c_tether Andras Low @ Aug. 7, 2015, 1:51 p.m.


    The library is now available through apt-get Thomas Macpherson-Pope @ Nov. 30, 2015, 3:18 p.m.

    Answer this question:

  • yup - same here , thanks Andras, instructions need revising ! jarjargeek @ Sept. 3, 2015, 3:38 p.m.


    Answer this question:

  • Im getting this error, Im using wheezy... No such file or directory: '/dev/i2c-1' — victagayun @ Nov. 7, 2015, 1:37 a.m.


    Answer this question:

  • should go to https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c and add manually kernel... — victagayun @ Nov. 7, 2015, 2:46 a.m.


    Answer this question:

  • Ask a new question:

Test with an example

Download this example to your Raspberry Pi (right click Save Link As…)

Run the example your Terminal with the following command

python3 example.py

You will see an arrow pointing up-left on your CodeBug’s LED display!


3 Questions
  • I think that is necessary run the example as sudo python3 example.py Jiří Pech @ Nov. 6, 2015, 5:37 p.m.


    Answer this question:

  • Didnt work for us... pi@raspberrypi ~/codebug $ sudo python3 example.py Traceback (most recent call last): File "example.py", line 6, in <module> with codebug_i2c_tether.CodeBug() as codebug: File "/usr/lib/python3/dist-packages/codebug_i2c_tether/i2c.py", line 46, in __enter__ self.open() File "/usr/lib/python3/dist-packages/codebug_i2c_tether/i2c.py", line 61, in open posix.O_RDWR | extra_open_flags) OSError: [Errno 2] No such file or directory: '/dev/i2c-1' Freddy Slade @ March 10, 2016, 4:08 a.m.

    • this looks like it can't find you codebug. have you enabled I2C, loaded your bug with the tether project and plugged it onto the GPIO correctly? — Thomas Macpherson-Pope @ April 7, 2016, 8:32 a.m.


    Answer this question:

  • this perticular program is not working with my pi + codebug. file"<filename>", line1 python 3.4.2 (default, oct 19 2014, 13:31:11) ^ SyntaxError: invilid syntax' Bhavik Bhansali @ March 29, 2016, 12:41 p.m.


    Answer this question:

  • Ask a new question:

Creating Tethered CodeBug programs

You can write your own Tethered CodeBug programs using Python and a few simple commands to control your CodeBug. In the next steps you will start an interactive Python session and enter commands to interact with your tethered CodeBug.

Open a Terminal and type

python3

You will see the python prompt appear >>>. Now import the CodeBug I2C library by entering

import codebug_i2c_tether

cb = codebug_i2c_tether.CodeBug()

cb.open()

cb.set_pixel(2,2,1)

You will see the center LED light up on CodeBug.

Try setting a row of CodeBug’s LEDs at the same time

cb.set_row(3,0b10100)

You will see the third row of LEDs light up in the sequence you 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.

Write text on the CodeBug’s LEDs, using the command

cb.write_text(0, 0,'A')

An A will appear on the CodeBug LEDs

To write scrolling text on the CodeBug’s LEDs, first import the time module:

import time

You can now scroll text using the commands:

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.

Press Crtl D to exit Python.


3 Questions
  • Where am i supposed to put the 'import time'? Þorvaldur Tumi @ Dec. 25, 2015, 6:59 p.m.


    When writing a Python program, the imports are usually put at the beginning of the program, however when using an interactive Python session, you can import them whenever you realise they are needed (just keep them outside of loops and if statements) Thomas Macpherson-Pope @ Jan. 4, 2016, 1:58 p.m.

    Answer this question:

  • When I type cb.set_pixel(2,2,1) The python interpreter Gives error Traceback (most resent call last): File "<stdin>", line 1, in <module> TypeError: set_pixel() takes exactly 4 arguments (3 given) What to do?? ben.charlton745@btinternet.com @ Feb. 20, 2016, 11:50 a.m.

    • have the lines before run with no problems? import codebug_i2c_tether cb = codebug_i2c_tether.CodeBug() cb.open() — Thomas Macpherson-Pope @ Feb. 24, 2016, 10:03 a.m.

    • yes — ben.charlton745@btinternet.com @ Oct. 8, 2016, 1:15 p.m.


    Answer this question:

  • when I am try to scroll the text the command is not supported. I got Error such like..... File "<stdin>", line 2 cb.write_text(i, 0, 'Hello', direction="right") ^ IndentationError: expected an indented block any solution ??? Bhavik Bhansali @ March 29, 2016, 1:03 p.m.


    Answer this question:

  • Ask a new question:

What next?

You can get a full list of the commands available by typing the following into your interactive Python shell, with the codebug_i2c_tether library imported.

help(codebug_i2c_tether.CodeBug)

Write long programs for Tethered mode CodeBug by writing commands in your text editor and saving and running the file in the way you did with the examples earlier, (python yourfile.py).

Tethered mode gives your CodeBug access to the full computing power, functionality and network connectivity of your Raspberry Pi! Make the most of the variety of powerful yet easy to use Python modules allow your CodeBug to generate random numbers, create email alerts or even post to Twitter!


4 Questions
  • It is also possible use it with the Intel Edison wit instaled Ubilinux. You need the I2C Breakout Block. Then you need install next packages: sudo apt-get install python3 python3-pip then install: sudo pip-3.2 install codebug_i2c_tether You will connect CodeBug (without batterry) to the Intel Edison by the M-M cables. You will use on the CodeBug only 4 ports (VCC, GND, SDI/A and SCL) All others are the same as is described in this article Jiří Pech @ Nov. 18, 2015, 11:40 a.m.


    Answer this question:

  • Where can I say "help(CodeBug)"? Andras Low @ Aug. 7, 2015, 12:32 p.m.

    • on the raspberry pi? — joseph b @ Oct. 28, 2015, 8:42 a.m.

    • "by typing the following into your interactive Python shell" So in the terminal on your raspberry pi, start a python shell by typing python and pressing enter. Then type import codebug_i2c_tether. (If that creates an error you need to get the library, look somewhere higher up on this page). After this you can type help(codebug_i2c_tether.CodeBug) to get the list. — nielssw @ Nov. 13, 2015, 2:56 p.m.


    You can run this in your interactive Python shell. This step has been updated to reflect this. Thanks :) Thomas Macpherson-Pope @ Aug. 11, 2015, 1:43 p.m.

    Answer this question:

  • Can we access the I2C functionality as a host so that the board could read a RTC or drive an external module? Andy Clark @ Oct. 1, 2015, 9:30 a.m.


    Answer this question:

  • Would it be possible to have the commands that help(..) lists somewhere on this website, as documentation? I'll post the basic ones here without descriptions: clear() get_col(col) set_col(col) get_row(row) set_row(row) get_pixel(x,y) set_pixel(x,y,state) write_text(x,y,message, direction='right') get_input(input_index) # 'A' or 'B' set_output(output_index, state) # 1 or True is on, 0 or False is off set_leg_io(leg_index, direction) #1=input, 0=output nielssw @ Oct. 13, 2015, 4:43 p.m.

    • try posting this to the website creators,they will be more likely to respond. — joseph b @ Oct. 28, 2015, 8:45 a.m.

    • try posting this to the website creators,they will be more likely to respond. — joseph b @ Oct. 28, 2015, 8:45 a.m.


    Answer this question:

  • Ask a new question:

Back to top