skip to Main Content

Tools Needed:

This video assumes you’ve got a Raspberry Pi that will work over Wi-Fi, a power source for the Pi, and that you’ve configured a microSD card for the Pi, installing latest version of Raspbian Lite to work over Wi-Fi. If you haven’t performed these steps, see: https://gallaugher.com/makersnack-setup-a-raspberry-pi-without-a-keyboard-or-mouse-headless-install/

This tutorial was written to install CircuitPython on a Pi as part of follow-along videos for building an iOS-controlled Raspberry Pi robot, but the steps below can be used to set up any Raspberry Pi on a network requiring a single password. The tutorial assumes you’re using a Mac (which will be needed if you’re going to use write an iOS app to control the robot).

Video:

Prefer video instruction? Use the follow-along YouTube video that covers the steps, below.

Step 1: Launch Terminal and Log Into Your Pi

  • Launch the Terminal program on your Mac by pressing Command-Space to launch Spotlight, then type Terminal, and press the return key.
  • Log into your Pi using the command below replacing hostname with your Pi’s hostname, then press the return key.
    ssh pi@hostname.local
  • Enter your Pi’s password when prompted, and press the return key.

Step 2: Install / Upgrade pip3 on your Pi

We’ll install pip3, which is a tool to install additional python software packages from the popular Internet site: Python Package Index (PyPI). To setup pip3 on your Pi perform these steps:

  • From the command prompt in terminal, enter the command below and press the return key.
    sudo apt-get install python3-pip
    Whenever you’re prompted to confirm if it’s OK to use additional disk space, type “Y” and press the return key, and installation will resume.
  • Now enter the command below and press the return key. Note: This installation might look like it’s frozen during the first minute or two of operations. Be patient and things should start to move on the terminal screen.

    sudo pip3 install --upgrade setuptools

Step 3: Install and Configure I2C

I2C (often pronounced “eye-squared-see”) stands for Inter-Integrated Circuit, and it’s a popular standard that allows a Raspberry Pi (as well as other devices) to communicate with things that are attached to it, like motors and sensors. To configure I2C on our Pi:

  • Enter the command below at the terminal prompt, then press return:
    sudo apt-get install -y python-smbus
  • When the installation above is done and you’ve been returned to the command prompt, enter the command below at the terminal prompt, then press return:
    sudo apt-get install -y i2c-tools
  • Run the raspi-config tool by typing the following at the terminal prompt, then press return:
    sudo raspi-config
  • Press the down arrow on the keyboard until “Interfacing Options” is highlighted, then press return.
  • Press the down arrow until I2C is highlighted, then press return.
  • When asked if you want to enable I2C, select <Yes> (use the arrow keys to highlight <Yes> if it’s not already selected), then press return.
  • Press return again on the highlighted <OK> confirmation.

You’ll be returned to the main menu for raspi-config.

Step 4: Install and Configure SPI

SPI (sometimes pronounced “spy”) stands for Serial Peripheral Interface, and like I2C, it’s also a standard to allow a device to communicate with peripherals. To install SPI, while still in the raspi-config tool:

  • Press the down arrow on the keyboard until “Interfacing Options” is highlighted, then press return.
  • Press the down arrow until SPI is highlighted, then press return.
  • When asked if you want to enable SPI, select <Yes>, (use the arrow keys to highlight <Yes> if it’s not already selected), then press return.
  • Press return again on the highlighted <OK> confirmation.
  • Press the right arrow key twice to highlight <FINISH>, then press the return key, and raspi-config will close, returning you to the Terminal’s command prompt.

Step 5: Install Python Libraries

Now we’ll install some additional libraries for the Python programming language that will allow us to write code to access the GPIO (General Purpose Input/Output) pins on the Raspberry Pi. These are the dual row of pins running across the top of most late-model Pis. To install this software:

  • Enter the line below at the Terminal command prompt, then press the return key (this also might look like it’s hanging – be patient, the install should start with the Terminal showing progress messages in a minute or two):
    pip3 install RPI.GPIO

Adafruit Blinka is a software tool that allows us to run Adafruit’s CircuitPython libraries (originally used just on the firm’s Arduino-compatible products) on a Raspberry Pi. It’s a really well supported set of Python libraries (for questions, see forums.adafruit.com the excellent support on these forums is just one of the many reasons to buy products from Adafruit), and we’ll use this in our robot project. Blinka is also the name of CircuitPython’s purple snake mascot. To install Blinka:

  • Enter the line below at the Terminal command prompt, then press the return key (this one might also take a bit of time before showing any progress – be patient):
    pip3 install adafruit-blinka

Step 6: Reboot the Pi, Log In Again

  • Reboot  your Pi by entering the command below, at the command prompt, then press the return key.
    sudo reboot
    It may take your Pi about 30 seconds to reboot.
  • Log back into your Pi using the ssh command, replacing hostname with your Pi’s hostname. Tip: pressing the up arrow key should show this command at the prompt, and you can then just press enter to log in.
    ssh pi@hostname.local
    • If you get a warning message stating that the Pi is unrecognized, just type “yes” and press the return key.
  • Enter your password and press the return key to complete the log in and return to the command prompt.

Step 7: Create and Run a Python Program to Test This Setup

Now let’s enter a Python program to test our installation. 

  • Create a blank file called blinkatest.py using the nano text editor by typing the following command at the Terminal command prompt, then press the return key:
    nano blinkatest.py
    This creates a blank file named blinkatest.py
  • Now copy the code below (it can also be found at: https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi
    import board
    import digitalio
    import busio
    print("Hello blinka!")

    # Try to great a Digital input
    pin = digitalio.DigitalInOut(board.D4)
    print("Digital IO ok!")

    # Try to create an I2C device
    i2c = busio.I2C(board.SCL, board.SDA)
    print("I2C ok!")

    # Try to create an SPI device
    spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
    print("SPI ok!")

    print("done!")
  • Type Control-X to quit nano.
  • Type “Y” when asked if you’d like to save the file.
  • Press the return key after the file name and you’ll be returned to the command prompt.
  • Now execute the blinkatest.py program by entering the command below, followed by the return key:
    python3 blinkatest.py
    If everything works, you’ll see the print statements have printed without any errors:
    Hello blinka!
    Digital IO ok!
    I2C ok!
    SPI ok!
    done!

If you’ve completed the steps above as part of the Let’s Build a Robot project, we’re ready to attach our motors to the Pi, and test them. There are versions of the next step instructions for projects using the Adafruit DC & Stepper Motor HAT for Raspberry Pi – Mini Kit as well as the WaveShare Motor Driver HAT for Raspberry Pi, I2C Interface. Find the next document in this series and links to accompanying step-by-step YouTube videos at: https://bit.ly/iOS-Pi-Robot

If you’re following this tutorial as part of the iOS-controlled “Make It Talk” project (Talking Yoda), return to the MakeItTalk project start page.

❖    ❖    ❖    ❖    ❖

Ready for more? Check out tutorials on Robotics, Wearables, App Development and more at: http://bit.ly/GallaugherYouTube. Please consider subscribing and do share with others who may be interested!

Want to learn to build apps? The same content I use in my semester-long online course (videos and tutorial content) is available for less than $25 via links you’ll find at https://gallaugher.com/swift.

Look for more updates at gallaugher.com as well as on Twitter @gallaugher

Back To Top