November 09, 2015 | Electronics |
Diwali (or deepavali, the Indian festival of lights) is just around the corner, and in addition to stuffing myself with unhealthy snacks and scaring my kids with firecrackers, it’s also the time to receive cheap LED lamps as gifts. ;-) So I decided to take one apart and make it controllable via a phone - using my favorite Nordic nRF51822 BLE SoC.
Before you read further, you might want to look through some of my previous articles on nRF51822 programming, since we’re going to use similar concepts and development setup here.
- nRF51822 Begins - nRF-DK, GCC, ADC, UART/BLE
- External nRF51822 SWD Programming using the nRF51-DK
- nRF51-DK PWM & GPIOTE test with S110 SoftDevice
- Talking to Ultrasonic Distance Sensor HC-SR04 using nRF51822
- Controlling an RGB LED with Nordic nRF51-DK (nRF51822/nRF51422)
- Motor Control over BLE with nRF51822 and TB6612FNG
- BLEBot - nRF51822 based BLE Robot
Hardware & Connections
This project uses one of the Chinese nRF51822 modules that are available from aliexpress for about 5 USD. In my case, I actually fried the chip in mine, and my friends at ExploreEmbedded helped me replace it with one of the free nRF51422 samples I got with my nRF51-DK board. My previous articles (above) cover programming this board, so I will not repeat the information here.
Here’s what the inside of the lamp looks like:
It’s just 3 x 1.5 V coin cells connected to an LED and a switch under the lamp. The first thing we need to do is reduce the voltage to 3 V by taking out one battery. I used a mutilated Lego piece and some hot glue to reposition the battery contact. The next thing to do is to create some space for the nRF51822 board, for which I had to flip the lid of the lamp.
The nRF51822 is connected as follows:
|P0.28||Cathode of LED|
The anode of the LED is connected to +3V - so here, we are using the LED in active low mode just the nRF51-DK. You need to set P0.28 to LOW to light the LED. For the connections, we have to solder wires to the annoying 2x9 headers:
Here’s what it looks like all hooked up:
Now, we need to program it.
The program starts with the usual boilerplate to set up BLE, and then goes on to set up a PWM channel as follows:
In the above code, pin 28 is setup with an active low PWM signal with a period of 5000 us (200 Hz). Now here is the main loop:
In the above loop, the PWM duty cycle is changed every step to make the LED “breathe”, and by changing the delay, we can control the rate of breathing.
We use the Nordic UART Service (NUS) to send commands to the chip via BLE. Here, you can see the data structure used for events, and how it’s handled.
The NUS commands are sent using the Nordic nRFToolBox app.
Here’s the hacked LED Lamp in action:
You can get the complete source code for this project here:
We love hearing from our readers. Email us at email@example.com for questions or comments on this article. If you found this article useful, please support us by buying some of our Open Source hardware products - like ZeroDriver - an Arduino Zero compatible motor driver for robotics. ZeroDriver is crowdfunding right now. Please click here to support our campaign. and bring this product to market!