Using the Raspberry Pi as a smart alarm clock

Lead Image © Gennady Kireev, 123RF.com

A Raspberry Pi Sunrise

A Raspberry Pi alarm clock can get you gently out of bed each morning with a simulated LED sunrise and custom alarm tones.

True, you probably don't need to build your own alarm clock, because conventional ones are easy enough to find and buy. However, a Raspberry Pi alarm clock can offer a lot more than a conventional clock. For example, with a Raspberry Pi alarm can choose your own music, design a web front end, and create a simulated sunrise with a color LED strip.

The instructions presented here offer a step-by-step guide for a basic alarm clock setup. The process starts with an LED strip that can be controlled via the PWM controller of the Raspberry Pi. Then, the project deals with the core task of waking you up. Because the PWM controller will be taking care of the LED strip, you need an extra sound card. Additionally, services like MySQL and Apache need to be installed. Finally, a series of small scripts ties everything together into a smart package.

Controlling the LED Strip

A variety of LED strips are available for purchase from hobby stores and online merchants (see the "Suppliers" box). Two major differences distinguish strips. The strips are characterized primarily by the number of LEDs per meter and the type of controller used. The strip I used for this project has more than 60 LEDs per meter and uses the WS2812B integrated LED and driver chip as a controller [1]. Each of the LED elements can be controlled individually and accept 16 million color values. The number of color values derives from combinations of red, green, and blue light, which emanate from three LEDs integrated into each element. The required data is transmitted serially via a special protocol that is dependent on correct timing. The nifty wiringPi library helps set up control of the LEDs via software [2].

Suppliers

eBay and AliExpress offer low prices for microcontrolled LED strips, but if you don't want to wait four weeks for delivery from China, you will have to look for a local source of delivery. For example, Adafruit has a good selection [6].

Theoretically, there is no difference between setting up a Rasp Pi alarm clock on a first-generation Raspberry Pi or a new Raspberry Pi Model 2 (RPi2). Practically speaking, however, as of mid-May 2015, the WS281x library [3] does not work on the RPi2 because of a bug [4].

The LED strip used for the project described in this article is 5 meters long. When every LED is lit to the maximum brightness, the strip draws an impressive 18A of power. The calculation for the amperage is

18A=5m x 60 LEDs/m x 3 LEDs/module

x 20mA/LED

The manufacturer data sheet gives the maximum power consumption as 90W (18A x 5V). Obviously, this much power places too high a burden on the GPIO port of the Raspberry Pi; thus, an external power supply unit is absolutely necessary.

You should heed the standards provided by the manufacturer and use an appropriate power supply unit even though the power demand for the run of the example program strandtest.py found in the WS281x library proved to be much lower during the first tests I conducted with a laboratory power supply. Additionally, it is best to provide power to both ends of the LED strip. Otherwise, the lights at the ends of the strip will be significantly less bright. This also explains why the LEDs do not shine with the maximum brightness when used for the Rasp Pi alarm clock.

For the first test setup, you should connect the data line (Figure 1) directly to GPIO1 (header pin 12) on the Rasp Pi because this is the only output that offers genuine PWM functions. (Note that I am using the wiringPi pin numbering scheme [5].) Figure 2 shows the circuit diagram for this. The layout also contains a switch that is used to turn off the alarm. The WS2812B works well with the 3.3V levels on the Raspberry Pi. It would be necessary to adapt these for most of the other controller components.

Figure 1: The test project is easy to set up.
Figure 2: The circuit diagram for this project is straightforward.

Installing Software

Once you have the hardware set up, you can begin installing the software needed for the LED strip (Listing 1). Make sure you have the most updated version of the software (lines 1 and 2). Then, install a series of Python tools and the command-line front end for Git (line 3). You can download the library for the control of the WS2812B as source code and construct it with SCons [7] (lines 6 to 8). Next is the installation of the resulting wrapper function for Python (line 10).

Listing 1

Installing the Software

01 $ sudo apt-get update
02 $ sudo apt-get dist-upgrade
03 $ sudo apt-get install build-essential python-dev git scons swig git-core
04 $ sudo apt-get install vorbis-tools mpg123
05 $ mkdir alarm && cd alarm
06 $ git clone https://github.com/jgarff/rpi_ws281x.git
07 $ cd rpi_ws281x
08 $ scons
09 $ cd python
10 $ sudo python setup.py install

After completing the installation of essential software components, you should test a loosely wired LED strip with the strandtest.py script from the ~/alarm/rpi_ws281x/python/examples directory. Before starting the test, modify the LED_COUNT value in the script to indicate that all of the LEDs should light up. In this project, the number of LEDs should be set to 300. Next, you can call the script with root rights:

$ sudo python strandtest.py

To get the LED strip to function as a simulated sunrise, you need four small C programs. Without these programs, the strip works like a chaser light. (See the "Brightness" box for more information.)

Brightness

The program all_on (Listing 2) turns on all of the LEDs at a little less than half of the maximum brightness. Turning the brightness of the LEDs up results in a drop in voltage inside of the strip and a noticeable darkening in the lights farther away from the power source. The only way to make sure all of the lights can shine at maximum brightness at the same time is to supply more power to the light strip by adding connections.

Listing 2

All On

// all_on
int main(int argc, char *argv[]) {
  int ret = 0;
  setup_handlers();
  if (ws2811_init(&ledstring)) {
    return -1;
  }
  int i;
  for (i = 0; i < LED_COUNT ; i++) {
    ledstring.channel[0].leds[i] = 0x999999;
  }
  if (ws2811_render(&ledstring)) {
    ret = -1;
  }
  return ret;
}
  • all_on switches the LEDs on with 100 percent white light (Listing 2).
  • all_off switches off the LEDs (Listing 3).

Listing 3

All Off

// all_off
int main(int argc, char *argv[]) {
  int ret = 0;
  setup_handlers();
  if (ws2811_init(&ledstring)) {
    return -1;
  }
  int i;
  for (i = 0; i < LED_COUNT ; i++) {
    ledstring.channel[0].leds[i] = 0x000000;
  }
  if (ws2811_render(&ledstring)) {
    ret = -1;
  }
  return ret;
}
  • soft_on increases LED brightness from 0 to 100 percent over five minutes (Listing 4).

Listing 4

Soft On

// soft_on
int main(int argc, char *argv[]) {
  int ret = 0;
  setup_handlers();
  if (ws2811_init(&ledstring)) {
    return -1;
  }
  long b;
  for (b = 0; b < 99 ; b++) {
    int i;
    long x=(b*65536l)+(b*256l)+b;
    for (i = 0; i < LED_COUNT ; i++) {
      ledstring.channel[0].leds[i] = x;
    }
    if (ws2811_render(&ledstring)) {
      ret = -1;
    }
  sleep(2);
  }
  return ret;
}
  • soft_off lowers LED brightness from 100 to 0 percent over five minutes (Listing 5).

Listing 5

Soft Off

// soft_off
int main(int argc, char *argv[]) {
  int ret = 0;
  setup_handlers();
  if (ws2811_init(&ledstring)) {
    return -1;
  }
  long b;
  for (b = 99; b > -1 ; b--) {
    int i;
    long x=(b*65536l)+(b*256l)+b;
    for (i = 0; i < LED_COUNT ; i++) {
      ledstring.channel[0].leds[i] = x;
    }
    if (ws2811_render(&ledstring)) {
      ret = -1;
    }
  sleep(2);
  }
  return ret;
}

The listing fragments shown here include only the main methods. Complete listings are on the Raspberry Pi Geek anonymous FTP site [8]. It is best to build the programs directly in the~/alarm/rpi_ws281x directory where the libws2811.a library for the LEDs resides (Listing 6). The compiled programs go into the directory where the scripts look for them (Listing 7). You should be careful when starting the programs, because the underlying library needs root rights; thus, the alarm programs need to be called with the sudo prefix.

Listing 6

Compile the Programs

$ cd ~/alarm/rpi_ws281x
$ cc all_on.c libws2811.a -o all_on
$ cc all_off.c libws2811.a -o all_off
$ cc soft_on.c libws2811.a -o soft_on
$ cc soft_off.c libws2811.a -o soft_off

Listing 7

Copy the Programs

$ mkdir /home/pi/alarm/sound
$ mkdir /home/pi/alarm/light
$ cp all_on /home/pi/alarm/light
$ cp all_off /home/pi/alarm/light
$ cp soft_on /home/pi/alarm/light
$ cp soft_off /home/pi/alarm/light

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content