Build­ing the voice-con­trolled wire­less LED light

Dar­ren Yates con­tin­ues his com­bined Python/Ar­duino/An­droid project, build­ing the hard­ware us­ing a Blue­tooth trans­ceiver and tiny Ar­duino Nano mi­cro­con­troller.

APC Australia - - Contents -

This month, we’re com­bin­ing Ar­duino, Python and An­droid to cre­ate a wire­less light switch con­trolled by com­bin­ing Blue­tooth, your An­droid smart­phone and your voice. Your An­droid phone pro­vides the speech recog­ni­tion and wire­less con­trol, Python pro­vides the code to ac­ti­vate the speech recog­ni­tion and Blue­tooth com­mu­ni­ca­tion, but here, we de­tail the Ar­duino hard­ware, in­clud­ing the Blue­tooth mo­d­ule and LED light.

HC-06 BLUE­TOOTH

The Ar­duino Nano mi­cro­con­troller board we’ve based this project on doesn’t have wire­less con­nec­tiv­ity built in, so we pull in the com­pact four-pin HC-06 Blue­tooth mo­d­ule. It’s easy to code and just like work­ing with a se­rial con­nec­tion.

There’s one con­di­tion, how­ever — it’s de­signed to run on any­thing from 3.6V to 6VDC, but can only deal in sig­nal lev­els of 3.3VDC. The sig­nal it sends to the Ar­duino (‘TXD’) is fine but it must re­ceive a 3.3VDC sig­nal in re­turn. Since the Ar­duino Nano only deals in 5VDC in­cre­ments, we need to im­ple­ment a sim­ple volt­age di­vider to drop the volt­age down. There’s a sim­ple equa­tion for work­ing out volt­age di­vi­sion with just two re­sis­tors: Vout = Vin x R2 / (R1 + R2) Here, we’re us­ing a 10kohm re­sis­tor for R1, a 20kohm re­sis­tor for R2 and Vin (the in­put volt­age) is 5VDC. Punch those into the equa­tion and you get 3.33VDC. If you don’t have a 20kohm re­sis­tor, you can sub­sti­tute an 18kohm re­sis­tor in­stead — this will give 3.21VDC, which will be fine. Most dig­i­tal in­puts have a tol­er­ance of +/-10% be­fore things start to wob­ble and we’re well in­side that.

SOFT­WARE SE­RIAL

The other is­sue we have with the Ar­duino Nano is that it only has one se­rial chan­nel — the RX and TX ports on pins D0 and D1. If we use th­ese to com­mu­ni­cate with the HC-06, we lose our abil­ity to com­mu­ni­cate with the PC over the Se­rial Mon­i­tor, which uses the

same se­rial chan­nel. The so­lu­tion is to cre­ate a sec­ond se­rial chan­nel, which we do in soft­ware, us­ing the ex­cel­lent AltSoftSe­rial li­brary. This turns pins D4 and D5 into a pseudo-se­rial chan­nel, which work just fine for our pur­poses.

For the most part, the HC-06 does its own thing — it waits for a pair­ing re­quest from the An­droid phone and fur­ther waits for a con­nec­tion re­quest from our Python code on the An­droid phone (that’s when the HC-06 comms LED stops flash­ing). Mean­while, our Ar­duino code is on con­tin­u­ous loop, ask­ing the HC-06 mo­d­ule ‘Any text mes­sage yet?’ When one fi­nally ap­pears, our code grabs the text, searches for the words ‘on’ and ‘off’ and sets the volt­age level on pin A0 ap­pro­pri­ately, turn­ing the LED on or off as re­quired.

LED OUT­PUT

We’ve used a low-cost high-bright­ness white LED as our ‘light’ for this project, con­nected to one of the ana­log in­put pins, A0, which we re­pro­gram as a dig­i­tal out­put. Ar­duino boards with the ATMEGA328P mi­cro­con­troller chip can source (pro­vide) or sink (ac­cept) up to 40mA of cur­rent on I/O pins and to en­sure we don’t blow up the Nano or the LED, we need to limit the cur­rent flow­ing through that LED. LEDs have what’s known as a ‘for­ward volt­age’ (Vf), which is the min­i­mum nom­i­nal volt­age re­quired across the LED pins be­fore any cur­rent will flow and any light ap­pears. For white LEDs, it’s roughly 3.5VDC — we say ‘roughly’ be­cause it’s not ex­actly pre­cise as it varies with dif­fer­ent LED makes.

To limit the cur­rent flow, we need a cur­rent-lim­it­ing re­sis­tor in series with the LED and again, there’s an­other equa­tion we use to be sci­en­tific about it and en­sure we get the right value of re­sis­tor: Rc = (Vin – Vf) / If Here, ‘Rc’ is the cur­rent-lim­it­ing re­sis­tor in ohms, ‘Vin’ is the 5VDC out­put from the A0 pin, ‘Vf’ is the nom­i­nal 3.5VDC for­ward volt­age drop across the LED and ‘If’ is the 10mA cur­rent flow we want through the LED. Again, dump those num­bers into the equa­tion and you get Rc =(5 – 3.5) / 0.01 = 150ohms. The im­por­tant thing to re­alise here is that th­ese num­bers have a lot of lee­way built in — all stan­dard 5mm LEDs are able to han­dle at least 20mA of cur­rent, but by aim­ing for only 10mA, we’re build­ing a safety mar­gin in case the for­ward volt­age drop of the LED, Vf, isn’t as high as 3.5VDC. If it’s lower, we’ll get more cur­rent flow­ing through the LED, but it won’t cause is­sues un­til we get more than 20mA of cur­rent flow. And for that to hap­pen, Vf will need to drop by half to 1.7V — and that’s not go­ing to hap­pen with a white LED, so the 150ohm value works nicely.

BUILD­ING THE LIGHT

Ev­ery­thing fits on a tiny 170-tie­point breadboard, with all parts pur­chased from eBay. Power is supplied through the miniUSB port, ei­ther via PC USB port or USB power adapter. Fol­low the over­lay di­a­gram and you shouldn’t have too many is­sues.

COD­ING THE SOFT­WARE

You’ll find the Ar­duino soft­ware re­quired for this project on the APC web­site at apc­mag.com/magstuff. Down­load the zip file, unzip it and copy the contents of the /li­braries sub­folder into the same of your Ar­duino IDE. Don’t have the Ar­duino IDE? Get it from ar­duino.cc/down­loads. Plug in the Ar­duino Nano into your PC, launch the IDE, open the voice­light.ino file, make sure you se­lect the Ar­duino Nano board and the cor­rect COM port in the ‘Tools’ menu — don’t se­lect COM1, that’s mouse. Flash the code to your Ar­duino, plug it into the breadboard and power up. If all is well, the comms LED on the Blue­tooth mo­d­ule will start flash­ing.

From here, fol­low this month’s Python Master­class for get­ting your An­droid phone up and run­ning to com­mu­ni­cate via Blue­tooth and speech recog­ni­tion.

MAK­ING THE IN­TER­NET OF THINGS

Mar­ket an­a­lysts fore­cast the In­ter­net of Things will ex­ceed 20 bil­lion de­vices by 2020. Mak­ing your own IoT de­vices is the best way I know of to learn how this stuff works and what it can be used for. Re­mem­ber, this project might only turn on and off an LED, but the same tech­nique with more pow­er­ful elec­tron­ics can turn on and off al­most any­thing you like. Once you’ve got the ba­sics, it only takes your imag­i­na­tion to cre­ate your own bril­liant ideas.

This HC- 06 Blue­tooth mo­d­ule links with your An­droid phone.

Make sure you set the cor­rect COM port to code your Nano. (Hint: not COM1.)

Our voice- con­trolled LED uses an Ar­duino Nano and Blue­tooth.

Use this over­lay di­a­gram to build your own ver­sion.

The Ar­duino Nano is a tiny 8-bit mi­cro­con­troller for mak­ing your own gear.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.