Building the voice-controlled wireless LED light
Darren Yates continues his combined Python/Arduino/Android project, building the hardware using a Bluetooth transceiver and tiny Arduino Nano microcontroller.
This month, we’re combining Arduino, Python and Android to create a wireless light switch controlled by combining Bluetooth, your Android smartphone and your voice. Your Android phone provides the speech recognition and wireless control, Python provides the code to activate the speech recognition and Bluetooth communication, but here, we detail the Arduino hardware, including the Bluetooth module and LED light.
The Arduino Nano microcontroller board we’ve based this project on doesn’t have wireless connectivity built in, so we pull in the compact four-pin HC-06 Bluetooth module. It’s easy to code and just like working with a serial connection.
There’s one condition, however — it’s designed to run on anything from 3.6V to 6VDC, but can only deal in signal levels of 3.3VDC. The signal it sends to the Arduino (‘TXD’) is fine but it must receive a 3.3VDC signal in return. Since the Arduino Nano only deals in 5VDC increments, we need to implement a simple voltage divider to drop the voltage down. There’s a simple equation for working out voltage division with just two resistors: Vout = Vin x R2 / (R1 + R2) Here, we’re using a 10kohm resistor for R1, a 20kohm resistor for R2 and Vin (the input voltage) is 5VDC. Punch those into the equation and you get 3.33VDC. If you don’t have a 20kohm resistor, you can substitute an 18kohm resistor instead — this will give 3.21VDC, which will be fine. Most digital inputs have a tolerance of +/-10% before things start to wobble and we’re well inside that.
The other issue we have with the Arduino Nano is that it only has one serial channel — the RX and TX ports on pins D0 and D1. If we use these to communicate with the HC-06, we lose our ability to communicate with the PC over the Serial Monitor, which uses the
same serial channel. The solution is to create a second serial channel, which we do in software, using the excellent AltSoftSerial library. This turns pins D4 and D5 into a pseudo-serial channel, which work just fine for our purposes.
For the most part, the HC-06 does its own thing — it waits for a pairing request from the Android phone and further waits for a connection request from our Python code on the Android phone (that’s when the HC-06 comms LED stops flashing). Meanwhile, our Arduino code is on continuous loop, asking the HC-06 module ‘Any text message yet?’ When one finally appears, our code grabs the text, searches for the words ‘on’ and ‘off’ and sets the voltage level on pin A0 appropriately, turning the LED on or off as required.
We’ve used a low-cost high-brightness white LED as our ‘light’ for this project, connected to one of the analog input pins, A0, which we reprogram as a digital output. Arduino boards with the ATMEGA328P microcontroller chip can source (provide) or sink (accept) up to 40mA of current on I/O pins and to ensure we don’t blow up the Nano or the LED, we need to limit the current flowing through that LED. LEDs have what’s known as a ‘forward voltage’ (Vf), which is the minimum nominal voltage required across the LED pins before any current will flow and any light appears. For white LEDs, it’s roughly 3.5VDC — we say ‘roughly’ because it’s not exactly precise as it varies with different LED makes.
To limit the current flow, we need a current-limiting resistor in series with the LED and again, there’s another equation we use to be scientific about it and ensure we get the right value of resistor: Rc = (Vin – Vf) / If Here, ‘Rc’ is the current-limiting resistor in ohms, ‘Vin’ is the 5VDC output from the A0 pin, ‘Vf’ is the nominal 3.5VDC forward voltage drop across the LED and ‘If’ is the 10mA current flow we want through the LED. Again, dump those numbers into the equation and you get Rc =(5 – 3.5) / 0.01 = 150ohms. The important thing to realise here is that these numbers have a lot of leeway built in — all standard 5mm LEDs are able to handle at least 20mA of current, but by aiming for only 10mA, we’re building a safety margin in case the forward voltage drop of the LED, Vf, isn’t as high as 3.5VDC. If it’s lower, we’ll get more current flowing through the LED, but it won’t cause issues until we get more than 20mA of current flow. And for that to happen, Vf will need to drop by half to 1.7V — and that’s not going to happen with a white LED, so the 150ohm value works nicely.
BUILDING THE LIGHT
Everything fits on a tiny 170-tiepoint breadboard, with all parts purchased from eBay. Power is supplied through the miniUSB port, either via PC USB port or USB power adapter. Follow the overlay diagram and you shouldn’t have too many issues.
CODING THE SOFTWARE
You’ll find the Arduino software required for this project on the APC website at apcmag.com/magstuff. Download the zip file, unzip it and copy the contents of the /libraries subfolder into the same of your Arduino IDE. Don’t have the Arduino IDE? Get it from arduino.cc/downloads. Plug in the Arduino Nano into your PC, launch the IDE, open the voicelight.ino file, make sure you select the Arduino Nano board and the correct COM port in the ‘Tools’ menu — don’t select COM1, that’s mouse. Flash the code to your Arduino, plug it into the breadboard and power up. If all is well, the comms LED on the Bluetooth module will start flashing.
From here, follow this month’s Python Masterclass for getting your Android phone up and running to communicate via Bluetooth and speech recognition.
MAKING THE INTERNET OF THINGS
Market analysts forecast the Internet of Things will exceed 20 billion devices by 2020. Making your own IoT devices is the best way I know of to learn how this stuff works and what it can be used for. Remember, this project might only turn on and off an LED, but the same technique with more powerful electronics can turn on and off almost anything you like. Once you’ve got the basics, it only takes your imagination to create your own brilliant ideas.
This HC- 06 Bluetooth module links with your Android phone.
Make sure you set the correct COM port to code your Nano. (Hint: not COM1.)
Our voice- controlled LED uses an Arduino Nano and Bluetooth.
Use this overlay diagram to build your own version.
The Arduino Nano is a tiny 8-bit microcontroller for making your own gear.