Reader small image

You're reading from  Wearable-Tech Projects with the Raspberry Pi Zero

Product typeBook
Published inJul 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786468819
Edition1st Edition
Languages
Right arrow
Author (1)
Jon Witts
Jon Witts
author image
Jon Witts

Jon Witts has been working within the IT industry since 2002 and specifically within Educational IT since 2004. He was introduced to Linux back in 2001 through his collaboration with two German artists who were visiting the arts organisation he was then working with. Having studied Fine Arts and Educational Technology and sought to innovate with open and accessible digital technologies within his creative practice, Jon is happiest when deconstructing technology and finding its limits. Jon has embedded within his school the use of Raspberry Pi computers, as an integral part of the delivery of the school's Computer Science curriculum as well as to run various school clubs and projects. Jon is a Raspberry Pi Certified Educator and also helps to organise and run the Hull Raspberry Jam events. I would like to thank my wife, Sally and our three daughters for putting up with all the cables and compoents around the house, and not least for being so tolerant of the need to dodge the robots racing round the kitchen floor!
Read more about Jon Witts

Right arrow

An LED Laptop Bag

In this project, we will attach an Adafruit DotStar LED strip to the front of a laptop bag, and we will then conceal our Pi Zero, the extra electronic devices required to control the LEDs, as well as all of the batteries required to run everything inside the pocket of the bag. Using Python, we will then write a program that will control our fully-colored, programmable LED strip to create different multicolored patterns on the front of our bag.

If you are using the same Pi Zero for this project as you did for the previous project, desolder the 15 cables that lead from the Pi Zero to the LEDs on the t-shirt, but leave your off switch and LED in place, as you may need to detach these from the case first. To deactivate the software running automatically, connect to your Pi Zero over SSH and issue the following command:

sudo systemctl disable twitterTshirt.service...

What we will cover

In this chapter, we will cover the following topics:

  • Creating our electrical circuit
  • Controlling our LED strip with Python
  • Adding our LED strip to the bag
  • Writing our main Python program
  • Making our program run automatically

Let's get on with things straight away and look at what we will need to complete this project.

The bill of parts

We will make use of the following things in this project:

  • A Pi Zero W
  • An official Pi Zero case
  • A portable USB battery pack with 2 A output (as high a mAh as you can find)
  • A portable USB battery pack for your Pi Zero
  • An Adafruit DotStar Black 144 LED 1 m strip
  • An Adafruit Perma-Proto quarter size
  • 74AHCT125 – Quad Level-Shifter (3V to 5V)
  • A 14 pin 0.3" IC socket
  • Solid core cable—black
  • Stranded core cable—red, black, yellow, and blue
  • Stranded core cable, 18 AWG size, red, and black
  • DIY USB male socket
  • Cable heat shrink
  • Small anti-static bag
  • A plain colored laptop bag
  • A Velcro to match your laptop bag in color
  • Soldering iron and solders
  • A hot glue gun
  • Volt meter

Creating our electrical circuit

Before we go any further with this project, we need to talk a little bit about power for our DotStar LED strip. DotStar LEDs have a very specific power requirement; if there is not enough power, you will not be able to see them clearly; and if there's too much power, you risk damaging your LEDs. To complicate matters further, we need our power source to be portable in this project, so we will need to make use of batteries of one kind or another. Due to the exacting power requirements of the DotStar LEDs, we will also need to use some electronics to allow our Pi Zero to control DotStar LEDs.

DotStar LEDs are rated as 5V devices. Including a +- 10% tolerance gives us a safe working voltage range of 4.5V to 5.5V to power our DotStar LEDs. Now, you may ask why we don't just use the 5V line from the Pi Zero to power the LED strip? The reason...

Controlling our LED strip with Python

Now that we have completed all of our electronics, we can test that we can control our DotStar LED strip using the Python programming language. Switch on your Pi Zero and connect to it via SSH as normal. First off, we will create a directory for all of our code for this chapter; type mkdir ~/WearableTech/Chapter6; and then move into the directory by typing cd ~/WearableTech/Chapter6.

We now need to enable the SPI interface on our Pi Zero. Start the Raspberry Pi Configuration tool by typing sudo raspi-config. From the menu that opens, select 5 Interfacing Options, and then in the sub menu select P4 SPI; then press tab to highlight Enable. Press Enter twice and then restart your Pi Zero by typing sudo reboot now.

Once your Pi Zero has rebooted, connect to it via SSH again, move into your newly created Chapter6 directory and then issue the following...

Adding our LED strip to the bag

We now need to attach our LED strip to our laptop bag. We are going to cut the strip into six smaller strips of 24 LEDs each. We will then solder cables to join the points back together to create a LED matrix, which we will attach to the front of our bag using Velcro.

Start by very carefully cutting your LED strip just after the 24th LED. You can use sharp scissors for this, but be sure to cut through the center of the four copper solder pads. Now, cut 5 lengths of your yellow and blue stranded cable and 5 lengths of your red and black 18 AWG cable. The length needs to be enough to join the LED strip back together; mine were about 5 cm. Once you have cut all of your lengths of cable, strip and tin each end.

Now, turn your LED strips over and secure the piece you are soldering to and your cable you are soldering with a pair of helping hands. To solder...

Writing our main Python program

Connect your USB battery to the LED matrix and power up your Pi Zero, and then connect to your Pi Zero via SSH. Move into the Adafruit_DotStar_Pi directory by typing this:

cd ~/WearableTech/Chapter6/Adafruit_DotStar_Pi

We are going to make our Python program in this directory as it contains the necessary files to connect to the DotStar strip with Python. Open a new file in Nano by typing this:

nano ledBag.py

In the empty file that opens, type the following:

#! /usr/bin/python

import time
from random import randint
from dotstar import Adafruit_DotStar

# set up our strip
numpixels = 144
strip = Adafruit_DotStar(numpixels, 12000000, order='bgr')

# start our strip
strip.begin()
strip.setBrightness(32)

In this first section of our program, we are importing our required libraries, setting up the details for our strip, and the initializing the strip...

Making our program start automatically

As with our previous projects, we now need to make our program start as soon as we switch our Pi Zero on.

First we will create our service definition file:

sudo nano /lib/systemd/system/ledBag.service

Now, type the definition into it:

[Unit]
Description=LED Laptop Bag Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/WearableTech/Chapter6/Adafruit_DotStar_Pi/ledBag.py

[Install]
WantedBy=multi-user.target

Save and exit Nano by typing Ctrl + O, followed by Enter, and then Ctrl + X. Now, change the file permissions, reload the systemd daemon, and activate our service by typing this:

sudo chmod 644 /lib/systemd/system/ledBag.service 
sudo systemctl daemon-reload
sudo systemctl enable ledBag.service

Now, we need to test if this is working so reboot your Pi by typing sudo reboot; and then when your Pi Zero restarts, you should see...

Summary

In this chapter, you learned the necessary electronics to get our 3.3V Pi Zero to control 5V items, and also we have done some pretty tricky soldering to cut and rejoin our DotStar LED strip into a matrix.

We looked at ways of attaching electrical components to fabrics, which we have not used before and also had to think in a bit more detail about our portable power sources!

We have finished up with a truly portable, and even if I say so myself, quite a stunning new bag for our laptop to be transported around in!

In the next chapter, we are going to be learning about Python programming in a little more detail, as we make use of our Pi Zero to create our own pedometer.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Wearable-Tech Projects with the Raspberry Pi Zero
Published in: Jul 2017Publisher: PacktISBN-13: 9781786468819
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jon Witts

Jon Witts has been working within the IT industry since 2002 and specifically within Educational IT since 2004. He was introduced to Linux back in 2001 through his collaboration with two German artists who were visiting the arts organisation he was then working with. Having studied Fine Arts and Educational Technology and sought to innovate with open and accessible digital technologies within his creative practice, Jon is happiest when deconstructing technology and finding its limits. Jon has embedded within his school the use of Raspberry Pi computers, as an integral part of the delivery of the school's Computer Science curriculum as well as to run various school clubs and projects. Jon is a Raspberry Pi Certified Educator and also helps to organise and run the Hull Raspberry Jam events. I would like to thank my wife, Sally and our three daughters for putting up with all the cables and compoents around the house, and not least for being so tolerant of the need to dodge the robots racing round the kitchen floor!
Read more about Jon Witts