Silicon ChipMax’s Cool Beans - April 2020 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Publisher's Letter
  5. Feature: NEWS
  6. Feature: Techno Talk by Mark Nelson
  7. Feature: Net Work by Alan Winstanley
  8. Project: Ultra-low-distortion Preamplifier with Tone Controls by John Clarke
  9. Project: iCEstick by Tim Blythman
  10. Back Issues: BACK ISSUES ON CD-ROMS by Jim Rowe
  11. Project: FLIP DOT DISPLAY by TIM BLYTHMAN
  12. Feature: Circuit Surgery by IAN BELL
  13. Feature: Practically Speaking
  14. Feature: Make it with Micromite by Phil Boyce
  15. Feature: AUDIO OUT by Jake Rothman
  16. Feature: Visual programming with XOD by Julian Edgar
  17. Feature: Max’s Cool Beans by Max the Magnificent
  18. PCB Order Form
  19. Advertising Index

This is only a preview of the April 2020 issue of Practical Electronics.

You can view 0 of the 80 pages in the full issue.

Articles in this series:
  • (April 2020)
  • (April 2020)
  • Techno Talk (May 2020)
  • Techno Talk (May 2020)
  • Techno Talk (June 2020)
  • Techno Talk (June 2020)
  • Techno Talk (July 2020)
  • Techno Talk (July 2020)
  • Techno Talk (August 2020)
  • Techno Talk (August 2020)
  • Techno Talk (September 2020)
  • Techno Talk (September 2020)
  • Techno Talk (October 2020)
  • Techno Talk (October 2020)
  • (November 2020)
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
  • Techno Talk (June 2025)
  • Techno Talk (July 2025)
  • Techno Talk (July 2025)
Max’s Cool Beans By Max the Magnificent Flashing LEDs and drooling engineers – Part 2 at. We started with a single toggle switch controlling a single unicolor LED (Fig.2.). We also noted that, in the UK, moving the actuator of a toggle switch down or up typically turns it On or Off, respectively (things work the other way in the USA, and you take your chances in the rest of the world. Using this setup, we explored a number of different scenarios. We started with the simplest case in which the LED tracks the state of the switch: Switch -> On; LED -> On Switch -> Off; LED -> Off Next, we added a bit of flash (no pun intended) by making the LED flash three times before going hard on when the switch was toggled to its On position: Switch -> On; LED -> Flash-Flash-Flash-On Switch -> Off; LED -> Off Fig.1. 450-piece box of LEDs from Amazon – remember, you can never have too many! Finally, we experimented with making the LED fade on and fade off, which actually looked surprisingly tasty: T Switch -> On; LED -> Fade-On Switch -> Off; LED -> Fade-Off he purpose of this miniseries is to cogitate and ruminate on how we can use LEDs to add to the pizazz of our hobby projects. In my previous column (PE, March 2020), we considered some of the things we could do with a single unicolor LED controlled by a single-pole, single-throw (SPST) toggle switch. Just for giggles and grins, if you are a little rusty in this area, you can peruse and ponder my column on Electrical Switch Terminology (https://bit.ly/30ZpToT). My initial experiments were performed using a green LED that I pulled out of my treasure chest of spare parts. Since we will be using a bunch of LEDs – and on the basis you can never have too many – I ordered a 450-piece set from Amazon for only $11.99 in the US or £8.99 in the UK (https:// amzn.to/38K7D5q). I remember when LEDs were rather new and relatively expensive, so being able to pick up a box containing five colors × 90 pieces for less than two UK pennies apiece never fails to amaze me (Fig.1.) Remember that we’re using an Arduino Uno with its 5V input/output pins for our experiments. All of the LEDs in my new goodie box are shown as having maximum forward currents of 20mA (ie, 0.02A). Furthermore, the red and yellow LEDs are shown as having a 2.0 to 2.2V forward-voltage drop. If we assume 2.0V, this means we’ll need current-limiting resistors of (5 – 2)/0.02 = 150Ω. Meanwhile, the white, blue, and green LEDs are shown as having a 3.0 to 3.2V forwardvoltage drop. If we assume 3.0V, this means we’ll need current-limiting resistors of (5 – 3)/0.02 = 100Ω. Happily, I have hundreds of both resistor values lying around. The mists of time Just to refresh our memories, let’s take a quick peek back through the mists of time to remind ourselves where we’re 66 Of course, we could play with a mixture of these effects. For example, I might be tempted to combine the flashing On from the second scenario with the fading Off from the third scenario (so many fun things to play with; so little time to do them all). Taking a timeout Sad to relate, I fear we have to take a bit of a timeout with one of the things we discussed in my previous column. This was doing something special if no one used the switch for a certain amount of time – say 10 minutes – which we called the ‘Inactive Timeout Period.’ We considered a variety of possibilities here, such as the LED slowly ‘breathing’ (fading On and Off) if the switch were in its On position, and periodically emitting a tiny flash if the switch were in its Off position. On reflection, however, I fear I was getting a little carried away here, because I was thinking of a single toggle switch and its accompanying LED in isolation. Suppose we have a control panel festooned with toggle switches and LEDs. If they were all breathing and/or flashing at the same time, the result would be a visual cacophony of chaos, and you don’t want to see one of those too often. System status On the other hand, let’s suppose that we have a master On/ Off control in the form of a pushbutton switch, for example. This could be equipped with a single green LED like our toggle switch. Alternatively, it could boast two LEDs, let’s say green and red (Fig.3). Practical Electronics | April | 2020 (a) Switch is On/Active (b) Switch is Off/Inactive Fig.2. Single LED’s active/inactive states. (a) Sy stem is On/Active is Off/Inactive Fig.3. System’s master On/Off pushbutton. As usual, there are lots of different things we could do here. For example, if the system is On, the green LED could be On and the red LED could be Off; contra wise, if the system is Off, the green LED could be Off and the red LED could be on. Funnily enough, following my first column, one of my friends – we’ll call him Tom (because that’s his name) – emailed me to say: ‘As a wee bairn of an engineer, I remember joking about adding an Off light to products. My fellow designers would chuckle at this, knowing it was intended to poke fun at the sales/marketing folks who were interested in new ‘features’ that the competitors didn’t have, regardless of how engineeringly senseless they were. When talking to sales/marketing, we positioned this as a product differentiator that would force our competitors to follow suit. ‘You can only imagine my surprise when this throwaway idea became real. These days, it is no longer fashionable to have an LED lit to indicate On and unlit to indicate Off. That would be too simple and pedestrian and not waste any extra power. Nowadays, you need an Off light illuminated, usually red, when the device is Off. Or, at least, mostly off, since you need actual power to light the Off light. You also need an On light, usually green, to indicate ‘Not OFF.’ I obviously missed my calling. I should have gone into sales/ marketing (heavy sigh).’ Now, I know that it may seem a little counterintuitive to have a special LED to indicate that the device is Off – obviously having the On LED turned Off performs a similar service – but having a special Off LED can be handy. When I plug a power supply into the wall, for example, I find it reassuring when its green LED lights up to indicate it’s seeing power, even if I’m not yet using it for anything. In a similar vein, in the case of something like a TV, having a red Off LED not only informs me that the TV is Off, it also informs me that the system is still receiving power and is poised to leap into action at my beck and call. Returning to the idea of an inactive mode; suppose the system is On, but no one does anything with it for a while. In this case, maybe the green LED associated with the system’s master On/Of pushbutton could enter a breathing mode, while all of the LEDs associated with the various toggle switches could either turn off completely or fade down to a low glow. Feel the attraction Another friend called Steve reminded me that pinball machines have something called the ‘attraction mode,’ in which an unoccupied game sings a siren song to passing punters using flashing lights and inviting sounds. I can easily see implementing this sort of thing on my own projects, like the Prognostication Engine pictured in my previous column. Assuming the beast is on, but no one operates it for the ‘Inactive Timeout Period,’ then the system’s master On/Off pushbutton LED could start breathing, and all of the LEDs associated with the toggle switches could fade down to a dull glow. Practical Electronics | April | 2020 (b) Sy stem (a) Switch is On/Active (b) Switch is Off/Inactive Fig.4. Toggle switch with two unicolor LEDs. Periodically, however, all of the toggle-switch LEDs could enter a period of frenzied activity to attract the attention of anyone who might be in the vicinity. Furthermore, we could add special ‘attraction mode’ sound effects. I’m also planning on giving the Prognostication Engine proximity detection so it knows if someone is close by and can respond accordingly. In the fullness of time, I’m planning on equipping it with AI and facial recognition, so it will only respond to commands from yours truly (at which point I’ll be ready to take over the world). One is the loneliest number In the early-1970s, the band Three Dog Night recorded a lot of great songs that still have me humming along to this day, including Joy to the World, An Old-Fashioned Love Song, and Never Been to Spain. On the other hand, they were also responsible for One is the Loneliest Number, which I loathe with a passion that defies mere words. Suffice it to say that this song makes me want to gnash my teeth and rend my garb. Having said this, they had a point when it comes to LEDs; one is a very lonely number. Two is a much happier quantity. So, let’s suppose that we now equip our toggle switches with two unicolor LEDs – one green and one red (Fig.4.) Let’s name our LEDs GLED and RLED. In this case, the simplest scenario would be to simply turn them On and Off as follows: Switch -> On; GLED -> On; RLED -> Off Switch -> Off; GLED -> Off; RLED -> On Alternatively, we could make things look a tad more sophisticated using simple fade effects: Switch -> On; GLED -> Fade On; RLED -> Fade Off Switch -> Off; GLED -> Fade Off; RLED -> Fade On Since we now have two LEDs, we could also modify our original power-on effect. If the switch is in its Off position when power is applied to the system, we could flash the red LED three times, then flash the green LED three times, then flash both LEDs three times, then fade both LEDs on, and finally fade the green LED off, leaving the red LED to indicate that the switch is currently Off. Alternatively, if the switch is in its On position when power is first applied, we could use a similar sequence, but start by flashing the green LED and end with the green LED illuminated. If you wish, you can download the sketch for the first scenario (https://bit.ly/2u3O7SV) and the second scenario (https://bit.ly/2ShNLjB), and also watch a video of these tests in action, including the power-on sequences discussed above (https://bit.ly/2tuOghW). Using these two LEDs, I’m sure you can come up with some interesting effects of your own. If so, please email me to tell me about them. For myself, I’m chomping at the bit to perform some experiments with bicolor LEDs, which will be the topic of my next column. 67 Over-extended Several folks have emailed me to ask about the small green PCB plugged into the breadboard in my videos. Well, I have a friend called Mike Pelkey. Believe it or not, Mike is the grandfather of BASE Jumping, where BASE is an acronym for building, antenna, span (bridge), and earth (cliff). In 1966, Mike and his friend Brian Schubert made the first parachute jumps from the top of the El Capitan mountain in California’s Yosemite National Park. Mike is also the founder of LogiSwitch.net and the creator of the LogiSwitch switch debounce chips I now use in all of my projects. A couple of months ago, we were chatting on the phone, and I was bemoaning the way I kept on inadvertently pulling out the flying leads associated with my Arduino-Breadboard prototyping projects. You can only imagine my surprise a couple of weeks later when Mike’s LogiSwitch Arduino Uno Workbench Proto-Extender Kit (try saying that ten times quickly) arrived in the post (Fig.5.). These little rascals are now available for purchase in the LogiSwitch store (https://bit.ly/36HS8cU). This board is a shield that plugs into an Arduino Uno. Observe the headers in the middle, which allow you to stack additional shields on top. What Mike has done is to take all of the Arduino’s digital and analogue input/output (I/O) pins – along with the 5V, 3V3, and GND pins – and break them out to three sides of the board. Underneath the board on these three sides are downwardpointing pins that you can plug into breadboards, thereby allowing you to create prototype projects that access the Arduino’s pins without using any flying leads. In the video accompanying this article, I’m using only one breadboard, but you can use two or three as required. Just to give you an idea as to how useful this can be, I created a video showing an Arduino driving 45 LEDs using fl ying leads (https://bit.ly/2u5Rg4I). I then created a second video showing the same Arduino using the Workbench board to Fig.5. LogiSwitch’s Arduino Uno Workbench Proto-Extender Kit. drive the same 45 LEDs without any flying leads whatsoever (https://bit.ly/31jjoNR). The additional components on the board include three tactile pushbuttons that are fed through a LogiSwitch debounce IC to three header pins, along with three LEDs and associated current-limiting resistors that are also fed to three header pins. You can use these switches and LEDs to help control and/or debug your projects. You can discover more about the switch debounce IC in this column (https://bit.ly/2Qxvqzr) and more about the Workbench in this column (https://bit.ly/37KPrIf). Cool bean Max Maxfield (Hawaiian shirt, on the right) is emperor of all he surveys at CliveMaxfield.com – the go-to site for the latest and greatest in technological geekdom. Comments or questions? Email Max at: max<at>CliveMaxfield.com Max’s Cool Beans cunning coding tips and tricks L ast month I explained that since I’m providing some downloadable Arduino sketches (programs) to accompany the current miniseries of Cool Beans column I thought it might be a good idea to provide a rationale for the coding style I use. In my previous column we talked about cases, spaces, comments, #define statements, and why using ‘magic numbers’ is a bad idea. Now let’s take a slightly deeper dive into things like naming conventions. Variable names A general rule of thumb is to use nouns or noun phrases (nouns with modifiers) for variable names because it makes the code easier to read. I use ‘camel case’ for my variable names. This means that compound words or phrases are written such that each word in the middle of the phrase begins with a capital letter with no intervening spaces or punctuation. In the case of global variables (which are declared outside of any functions and which are available to all functions), I use upper camel case (aka ‘Pascal case’), in which the first character is also uppercase; for example: 68 int MainLoopCounter bool MainDoneCounting = START_COUNT; = false; For local variables (declared inside a function and which are available only to the function in which they are declared), I use lower camel case, in which the first character is in lowercase: int localLoopCounter = START_COUNT; bool localDoneCounting = false; Rationale: I like the look of camel case and I find it conveys a lot of information in an easy-to-read format. Using upper and lower camel case for global and local variables, respectively, helps me know ‘what’s what’ when I’m reading my code. Function names With regard to my own functions (we have to use the Arduino’s setup() and loop() functions ‘as is’), I use the same upper camel case for function names as for global variables because I think of functions as being global. A general rule of thumb is to use nouns or noun phrases (nouns with modifiers) as names for Practical Electronics | April | 2020 functions that are ‘getters’ or ‘questioners’, and to use verbs or verb phrases for functions that are ‘doers’ or generate side effects. Functions that return Boolean (true/false) values usually have names that start with ‘Is’, ‘Has’ to make reading expressions easier. Function calls and declarations Consider the following function call and function declaration: // Body of program // Do stuff here UpdateCounter(); // Do stuff here // Body of program // Call function void UpdateCounter () { // Do stuff here } // Declare function Observe that I don’t include a space between the function name and the ‘()’ parentheses when I’m calling the function in the body of the program, but I do include this space when I declare the function. Rationale: This makes it easy to search the code for instances where the function is called (without space) versus the function itself (with space). Use of curly brackets: { } When declaring a function, some people place the opening ‘{’ immediately after the parentheses; for example: void UpdateCounter () { // Statements go here } By comparison, I prefer to have the opening ‘{’ on its own line directly above its corresponding ‘}’; for example: void UpdateCounter () { } I also follow the same practice with conditional statements: if (doneCounting == true) { UpdateCounter(); } Similarly, I follow the same practice for control structures like for () loops; for example: for (int i = 0; i < MAX_COUNT; i++) { // Do stuff here } Rationale: When you have nested statements, it can quickly become difficult to work out which ‘{’ goes with which ‘}’. Placing the opening ‘{’ on its own line directly above its corresponding ‘}’ makes it much easier to work out what’s going on. More on curly brackets: { } In the case of a conditional statement that has only a single associated action statement, some people omit the ‘{’ and ‘}’ altogether: if (doneCounting == true) UpdateCounter(); Practical Electronics | April | 2020 or if (doneCounting == true) UpdateCounter(); By comparison, I always use ‘{’ and ‘}’, even if I have only a single action statement; for example: if (doneCounting == true) { UpdateCounter(); } Rationale: When you are debugging code, you often want to add additional statements, and having the ‘{’ and ‘}’ already in place greatly aids this process while also preventing you from adding new problems while you are trying to resolve existing issues. Indentation Like many C programmers, I used to use two spaces for indentation; for example: void BigRedButton (int whatState) { if (whatState == BUTTON_OFF) { UpdateColor(OFF_COLOR); } else if (whatState == BUTTON_ON) { doUpdateColor(ON_COLOR); } else { } } Now, however, based both on my reading of the Barr Group’s Embedded C Coding Standard (https://bit.ly/2MP3ftB) and on my experiences with Python, I prefer to indent using four spaces; for example: void BigRedButton (int whatState) { if (whatState == BUTTON_OFF) { UpdateColor(OFF_COLOR); } else if (whatState == BUTTON_ON) { doUpdateColor(ON_COLOR); } else { } } Rationale: Using four-space indentation makes the code much easier to read and understand, both for the originator and for whoever has to work out what this all means at some time in the future. (There’s the old programmer’s joke about a comment found in code saying, ‘When I wrote this, only God and myself knew how it worked, now God only knows!’) Next time In my next column we’ll look at declaring variables as part of for () loops, naming control variables used in for () loops, and data types in general. 69