Eagle Labs in Cambridge to run not exclusively Raspberry Pi event – 6th February

eagle_labs_jam

Dr Lucy Rogers is organising a maker/hacker/Raspberry Pi event at Eagle Labs, which is the new Maker Space in Cambridge. The event will take place between 2-5pm on Saturday, 6th February. Sponsored by Barclays, the Maker Space has lots of equipment to use to bring your projects to life. During the event, you’ll be able to learn more about the Space and also take part in various activities, including a Raspberry Pi show-and-tell. There is parking on site. If you’d like to attend, or get involved by bringing along your own projects, please email cambridgeeaglelabs@barclays.com to let them know you’re coming (there’s a limit of 50 people for the event).

Aged 8 yrs old and up, under 16’s accompanied by parent or guardian, no soldering on site.

SenseHAT gets a new e-book from the Raspberry Pi MagPi

The MagPi has expanded their ‘Essentials’ range today with a new e-book that covers the SenseHAT. The SenseHAT is, of course, the add-on board that is currently on the International Space Station. The book looks great and should be a good guide for everyone with the HAT, especially teachers with it’s practical examples of how to program with the board. You can read more about it here (and get it via an App) or download the digital PDF edition here.

Live coding with Sonic Pi and Minecraft on the Raspberry Pi

The MagPi have published a tutorial that Sam Aaron, creator of Sonic Pi, wrote about live coding with his software. This tutorial has a twist, however, as it integrates with Minecraft: Pi Edition and allows you to build in the 3D world based on input from the musical environment of Sonic Pi. Well worth a look at if you want to captivate people with your creations. Read it here.

Fading LEDs using PWM with GPIO Zero on the Raspberry Pi

fading_leds

The project

GPIO Zero, the new GPIO library from the Raspberry Pi Foundation, is becoming my go-to method for doing simple things with the Pi’s GPIO pins. Over the past couple of weeks, I’ve been having fun preparing for the Bett exhibition, and more specifically the Raspberry Jam that is being held there on the 23rd January.

Part of my exhibit is a Raspberry Pi Zero with a Scroll pHAT attached. Getting that working was a story in and of itself, but I want to focus today on the 4 LEDs I soldered onto some prototyping board that is attached to the Zero. You can see them in the picture above.

Back to GPIO Zero. It contains a class called PWMLED which allows you to do software PWM with LEDs. What this means is that instead of just blinking them on and off you can fade them in and out. This takes a little bit of doing (unless I’ve missed something) so I thought I’d share with you my method. One thing to understand is that I’ve done it in a ‘thread’, which means that while the Scroll pHAT is doing it’s job of displaying messages, I can have the LEDs doing something disconnected from that process. The individual functions, however, could be removed from inside the threading ‘object’ if you don’t need to do anything else except pulse the LEDs.

Here’s a video to prove that it works!

The code

Anyhoo. Here’s the code. (I’ve removed anything to do with the Scroll pHAT) (You can skip this bit and go straight to the bold bit to download the code from Github.

First of all, let’s import the libraries:

import time, threading, random
from gpiozero import LED, PWMLED

Now, let’s define the LEDs:

# Set-up LEDs
led_red = PWMLED(17)
led_yellow = PWMLED(27)
led_green = PWMLED(22)
led_blue = PWMLED(10)

Now, in order to get a smooth fading action, we’re going to want to loop from 0 to 1 using a ‘float’ (decimal) value. (0.1… 0.2… 0.3… 0.4… etc). We can’t do that without a helper function. Note, Python doesn’t seem to do float subtraction very well, so I found I had to round the floats to one decimal place just to make it play nice! If I’m missing something, please leave a comment!

# Helper function to iterate over a float
def frange(start, stop, step):
  i = start
 
  if (start < stop):
    while i <= stop:
      yield i
      i += step
      # For some reason, += doesn't always add an exact decimal, so we have to round the value
      i = round(i, 1)
  else:
    while i >= stop:
      yield i
      i += step
      # For some reason, += doesn't always add an exact decimal, so we have to round the value
      i = round(i, 1)

Now let’s define a class that we can start as part of a new thread:

class RandomLEDs(threading.Thread):
  def __init__(self, threadID, name):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name

  def run(self):
    while True:
      led_list = [led_blue,led_yellow,led_green,led_red]
      the_led = random.choice(led_list)
      self.fade_in_led(the_led, 0.03)
      time.sleep(0.3)
      self.fade_out_led(the_led, 0.02)
      time.sleep(0.3)

  # PWM the LED value from 0 to 1 (or from 1 to 0) with a 0.1 step
  def fade_in_led(self, led, speed):
    for i in frange(0.0, 1.0, 0.1):
      led.value = i
      time.sleep(speed)

  def fade_out_led(self, led, speed):
    for i in frange(1.0, 0.0, -0.1):
      led.value = i
      time.sleep(speed)

Now, let’s kick the whole thing off:

# Start independent threads
# This one lights up random LEDs
thread1 = RandomLEDs(1, "Thread-1")
thread1.start()

I’m hoping that all the indents have come through okay (I just spent the last 10 minutes putting spaces in, so hopefully so).

TL;DR

If you want to download the code, do the following:

git clone https://github.com/recantha/scroller

And the file you want is pwm_leds.py

You can run it by doing:

python3 pwm_leds.py

Feedback

I’d be very happy to get any feedback you might want to give about my code, and my approach. I expect that there is a much easier way to do this – the float looping deserves a bit of consideration, for instance – but this is what I got working! In any case, I hope you found the post interesting! 🙂

The Pi Hut is selling cheap SD cards for the Raspberry Pi (or whatever use you have for them)

The Pi Hut is currently selling off some excess Kingston microSD cards (not as pictured) for low prices. Currently, Jamie has the following left:

  • 4GB Class 4 (£2) – especially useful for Jessie Lite!
  • 8GB Class 4 (£4)
  • 32GB Class 10 (£14)

Don’t delay if you want to get some – apparently they’re going fast (the 16GB ones he had have already gone). Go to The Pi Hut.