Tag Archives: Projects

LED bed light

I have been working on my own custom wakeup-light on and off for several years (part 1, part 2, part 3). After getting Philips Hue lights, however, I have not gotten around to setting it up in my new apartment. So recently, when the need came along, I figured the quarter round rod I used for that project might also make a pretty nice looking bed light.

Version 1

I cut one of the old rods to length and taped it to the headboard to see how it would look:

This could work! I borrowed the old driver board from the wakeup light and hooked up a couple of potentiometers to an Arduino to try the whole thing out:

Version 2

After convincing myself (and subsequently the significant other) that this was a good idea, I went at designing and 3D-printing some mounting brackets to replace the silver tape that held the v1 in place.

Prototype 1
Prototype 2
Final version

The last version was long enough to hold the rod and had holes to let the cable escape invisibly out the back of the headboard:

Looking way better already!

Version 3

The next thing to work on was the electronics. Using potentiometers and analog inputs was not really ideal as the light would flicker and change even by just touching the knobs or the cables and required frequent polling of the inputs in the code. Also, the potentiometers I had were only single-turn.

To make a more reliable control, I ordered some EC11 and EC12 “coded switches” or rotary encoders from Aliexpress. These turn just like potentiometers, but are really acting like switches, opening and closing in a sequence that lets you decide which way they are rotating. The main advantage of this is that there is no need to sacrifice an entire analog input channel just for the sake of having a rotary knob and furthermore, the pins can be hooked up to pin interrupts, removing the need for polling altogether. The ones I got could also be pushed like a button, which I could use to switch between brightness and color mode instead of having two separate knobs like before.

After getting the  encoders in the mail, I designed yet another version of the holder that would also house the control knob:

The knob looks a bit out of place and is bigger than is really needed. I will probably try making a smaller one that fits the style of the whole thing a bit better for version 4. And then it would also be neat if the light could be controlled over wifi of course… But first I will figure out a way to hide the electronics so that they are not on top of the headboard!

The  3D-printed parts I made fit the bed IKEA Malm really well but are fairly simple in design. If you still would like me to share them for your own project, drop a comment below!

Alarm Clock v0.1.0

Alarm clock prototype

We wanted to try banning phones from the bedroom (you should try, I recommend it!). Clearly, a suitable hardware replacing the alarm clock app was needed. Having thought about building my own alarm clock for a while, I quickly determined it was not a viable option to just go buy one – there simply did not exist a model with all the features I had thought of and now needed to have, like for example:

  • Weekly schedule (no alarm on weekends)
  • Smarter snooze (configurable and longer)
  • Integrated with wakeup-lights and the rest of the appartment
  • Configurable from other devices
  • Programmable/extendable with future ideas

Alarm clock prototype parts

For the first prototype, I used some parts I had laying around:

  • Raspberry Pi A+ with USB wifi dongle
  • 1.8″ TFT display (check Ebay for “HY-1.8 SPI”)
  • Some prototyping board, connectors and pushbuttons
  • Small speakers with 3.5 mm jack

The firstAlarmclock wiring step was to connect the display. The one at hand communicated over SPI, which all Pi’s support, and hooking it up was not too difficult. Then, however, I spend quite some time trying to make the Pi recognize it as a screen rather than handling the SPI commands to it directly in my code. (Doing that would mean the interface could be a webpage for example, which would make it easier to develop.)

Using an SPI TFT as a monitor had been achieved already and made quite a buzz on Hackaday back in 2012 or so, but unfortunately it was not so easy to reproduce. At the time of building this in late 2016, most documentation I could find was still from 2012-2013 and talked about compiling the kernel from scratch and a frame buffer driver called fbtft. But, once I found its official Github repository, the first thing in the readme was (and still is) a message from early 2015 saying the driver has moved into Linux staging and that development there has ceased:

I could not find any signs if fbtft is now actually part of Raspbian nor any comprehensible documentation on how to set it up so I ran out of patience and decided to go with direct SPI control for the first version, meaning less fancy graphics for now. For direct SPI, finding examples was a little easier and by learning from the code on w8bh.net, I finally got something working.

The Pi now runs a fairly simple Python script, listening to the buttons and updating the display every minute, playing an mp3 file at increasing volume if it is wakeup time. The display shows current time and the time of the alarm (which only runs monday – friday). Two of the buttons are used to move the alarm time back or forth in 15 minute intervals. This can be used to change the alarm, snooze or skip it in the morning. The third button stops the alarm and the fourth toggles the Philips Hue lights in the bedroom on/off. The Hue lights are actually controlled via MQTT, from a Node Red server running on a separate Raspberry Pi,  acting as a hub for this and some other “smart home” features which might be a topic for a future post.

Alarm clock interface

All-in-all we are pretty happy with this first version, it has been in live use for four months now without any major malfunctions and basically it just needs an enclosure. It does lack some obvious features, like adding an alarm on the weekend and actually moving the time the wakeup-light starts along with the alarm time. Also, when I make a new version, probably I will add another button for starting the coffee maker as well. 😁

Autonomous RC Racing

Here is a project I have been working with on and off for about a year – Alvin the autonomous RC car. The robot is built to comply with the rules of two Swedish robot competitions, Robot SM and Stockholm Robot Championship, where the objective is to race three other robots around a track without any form of remote control. Rules vary somewhat, but each heat typically lasts until one robot reaches 7 laps, or for max 3 minutes. The robots are then given points according to the number of laps they have completed at this point. The participants can also signal the organizer to flip or turn the robot if it crashes or gets stuck, at the cost of one point.

Here is Alvin in action, racing some other robots from the Norbot team at a recent meetup:

Parts

  • Wltoys A222 RC car
  • Texass Instruments TM4C123G Launchpad microcontroller evaluation board
  • My own custom Launchpad protoboard booster-pack
  • Sharp GP2Y0A21YK0F analog distance sensors from Ebay
  • “10A Brushed ESC Motor Speed Controller for RC Car without Brake” off Ebay
  • A3144 hall effect switch, also from Ebay
  • Rare earth magnets (D4x2mm), Ebay as well

Operation

Since Alvin is built on a RC car, both the steering servo and throttle ESC have a maximum update rate of once per 20 ms, or 50 times per second. For the first implementation, therefore, the processor simply runs the algorithms for calculating new steering and throttle values once every 20 ms and then just waits in between.

For the steering, Alvin uses two Sharp distance sensors to measure the distance to the side walls and a third to detect obstacles in front. To stay in the middle of the track, it simply compares the distance to the walls and compensates the steering to make them equal. This rather simple approach could probably be improved a lot to gain more speed.

Since the track contains a hump, the power to the motor can not be hardcoded – it needs to increase in the uphill. To control the speed, a hall effect sensor is reading two magnets mounted on the drive shaft. The algorithm converting pulses from this sensor to a throttle setting has so far proven to be the hardest to implement, mainly because there are only two pulses per revolution of the drive shaft. This means there are normally just 0-2 new pulses recorded  every time the algorithm runs, making it hard to determine the actual speed. To work around this, the delta in revolutions is instead calculated 10 iterations back in time. The resulting speed value is then used as input to a PI controller which calculates the throttle.

Results

Alvin raced for the first time last year at Stockholm Robot Championship and I was very happy to finish 6th without ever tuning it on a full size track before! I have since worked on the throttle algorithm to make it as responsive as in the video above, so I think it could do even better today.

Future improvements

  • Extend front bumper to go around the wheels to avoid getting stuck against the wall.
  • Add a speed sensor with more pulses per revolution to allow better throttle control.
  • Add more distance sensors to enable a more advanced steering algorithm.
  • Add roll cage or body to protect the electronics and improve the looks.

Conclusion

Autonomous robot racing is a fun and fairly affordable way to put your combined engineering skills to the test, including both mechanical, electrical and software components. Being new in my city, it has also been a way to meet like-minded people and a reason to go down to the local hackerspace.

Finally, here is a playlist from Stockholm Robot Championship 2016 if you want to see some more action!

Wake-up Light Part 3: Hardware

This is the third part (part 1, part 2) of my write-up on building a Raspberry Pi-based wake-up light. At this point, the Pi was capable of switching 433 MHz remote controlled outlets and can be scheduled to do this at certain times.

I had a few ideas on how to roll my own light for the project. In the end, I went for 10 m of RGB-LED strip on Ebay and four pieces of quarter round rod from the department store. I think these pictures speak for themselves:

2014-02-03 21.00.58 2014-02-08 21.51.332014-02-03 20.14.41 2014-02-08 19.51.592014-02-03 20.14.32 2014-02-03 20.36.15 2014-02-03 20.14.562014-02-08 22.42.13

To get off the ground quickly, I whipped out an Arduino and soldered three MOSFET transistors to a protoboard. My initial idea was to hook it up to the Pi via I2C, but as I had the 433 MHz receiver left over from the kit I bought, and also already being familiar with the library as I used it to find what codes to use for my outlets, I figured I might as well make it wireless. This also has the added bonus of making it possible to control the light directly with my existing remote.

2014-02-08 21.46.16

To test things out, I hastily modified the rc-switch library example. The code is available over on my Github account, although for the time being it really is not very pretty and does contain bugs! I made the A-channel on my remote turn the light on and off and used B to set the brightness. I also wrote a small state machine to dim the light from blue through red and orange and up to white at full brightness over the course of 15 minutes. This would be my wake-up sequence. The “E” channel does not have any buttons on my remote (only A-D). It is, however, no problem to send on and off commands on it using the commands on the Raspberry Pi. Making the Arduino listen to channel “E” of my outlet remote system meant I did not have to waste one of the four channels on the actual remote. To the Pi it was all the same, I just changed what channel it should power on in the crontab. Here is an attempt to catch a sped-up version of the light sequence on camera. You get the idea.

2014-03-04 21.02.06

Sweet, at this point the project was really starting to get somewhere! The wake-up sequence is good enough for now, and the extra feature of being able to toggle the light on and off along with my other lamps in the evening is very convenient. With the right setup, ssh, nano and cron can be quite ok for setting the alarm. However, I was hoping to do something more portable and… modern, like an MQTT message server perhaps?

Next time: Software!

Wake-up Light Part 2: Scheduling

Last time, we had managed to get a command for controlling the lights up and running on the Raspberry Pi. The obvious choice for scheduling such a command in Linux is Cron. To automatically turn on the lights using Cron, we need to edit the root-user’s crontab:

This will launch a text editor inside the terminal. Lets scroll down to the end using the arrow keys and add the following lines:

The values, in turn, represent minute, hour, day of month, month and day of week. That is, the first command fires 06:45 every month, Monday through Friday. The send command is the same as before; turn group 11111, light 3 and 4, on (1). There is lots you can do with Cron and it has been around forever. If you have not used it before there are lots of extensive guides out there to get you started so I will not bother. This setup took me through most of the winter. The Pi turned on the lights just before my alarm went off at 07:00 and the results were imminent – waking up was a lot easier and “softer” as the light eased the body into the idea of waking up before it was blasted by the sound of an alarm. It was good, but not what I had planned for. This was only fading in two discrete steps, and even though I could just have added a few more outlets like this, I really wanted a singe, more powerful, custom light. Plus, the relays in the outlets make a loud and annoying clicking sound when they turn on. On the software side things were working fine, but logging onto the Pi and setting the alarm in the terminal was not really optimal. Next time: Building some hardware!

Wake-up Light Part 1: Flipping switches

2014-02-08 22.45.39 2014-02-08 22.45.20 2014-02-08 22.45.31

I’ve had the idea to build my own wake-up light for quite some time. This winter i finally got around to it. This is the first part of my write-up of the project.

In simple terms, the point of a wake-up light is to gently wake you up by slowly turning up the light before the alarm goes off in the morning. There are several commercial products available that do this already, all of which I think are either ugly, too small or simply not practical enough for my use case. Some of the things I wanted that are commonly not available in these products are:

  • Big, powerful light. Usually, products are night stand-size alarm clocks with a few measly watts of power.
  • RGB controllable light. My living room is also my bedroom. If I’m going to make the wake-up light big and powerful, I also want to be able to set it to a cosy warm evening-setting. Plus, this would make it able to do a more sunrise-like color fade in the morning.
  • Powerful scheduling options. I’m a student and don’t get up at regular hours every day. I want the wake-up time to be easily settable, preferably synchronized to my alarm or even my schedule.

Flipping some switches

I figured I could use my Raspberry Pi as the central point for the system. It’s hanging off the back off my TV, running Raspbmc and is on 24/7 anyway, so a simple start would be to add a cronjob to simply turn on some lights in the morning. I already had 433 MHz light switches connected to three ambient lights in my room, so I started out with by hooking up a transmitter to the Pi.

2013-10-12 22.51.17

Having someone in Shenzen send me a transmitter/receiver pair for 433 MHz on Ebay set me back about the same amount sending a normal letter home to my mother would. Mine was titled “RF Wireless Transmitter & Receiver Kit Module 433Mhz for Arduino/ARM/MCU WL”.

rf

Obviously, someone on the Internetz already wrote the code to talk to my outlets using Arduino. And someone else also ported it to Raspberry Pi. Sweet! The project I’m using is called rcswitch-pi. According to the readme, it depends on the (seemingly useful) wiringpi project, so first we need to get that (assuming you have git installed):

Wiringpi is used for the gpio calls inside rc-switch and is sort of replacing the Arduino’s digitalWrite() and the like. Now let’s install rcswitch-pi:

The program is hardcoded to have the transmitter connected to Wiringpi pin 0, which corresponds to pin 11 of the GPIO header on the Pi. If you want, you can change this by editing send.cpp and running the make command again.

Hooking up the receiver to my Arduino and using the original rc-switch library, I figured out what group and channel corresponded to the buttons on my remote.

2013-10-12 22.50.14

I could now control the lights from the Pi using these commands:

A small explaination to all these numbers: my outlets have “group switches” allowing different remotes to control  different sets of outlets. I have mine set to 11111. The buttons on the remote are labeled A-D. I have some lights hooked up to C (3) and D (4). All this I learned by loading the example sketch from the rc-switch library onto my Arduino and observing the serial output as I pushed some buttons on the remote.

Once I knew the send command was working, I linked it into /usr/bin/. This makes it possible to run the command without having to cd into the folder every time, which will be convenient for scheduling later.

Next time: Scheduling!