Silicon ChipArduino Bootcamp – Part 10 - October 2023 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions
  5. Back Issues: Hare & Forbes Machineryhouse
  6. Publisher's Letter: Time for some new PICs
  7. Feature: Holy Spheres, Batman! by Max the Magnificent
  8. Feature: Net Work by Alan Winstanley
  9. Project: Automatic Level Crossing and Semaphore Control by LES KERR
  10. Project: Multi-Stage Buck-Boost Battery Charger by Tim Blythman
  11. Project: PIC & AVR Chips from Microchip by Tim Blythman
  12. Project: PIC AND AVR Breakout Boards by Tim Blythman
  13. Feature: Arduino Bootcamp – Part 10 by Max’s Cool Beans
  14. Feature: AUDIO OUT by Jake Rothman
  15. Feature: KickStart by Mike Tooley
  16. Feature: Circuit Surgery by Ian Bell
  17. PCB Order Form
  18. Advertising Index by Ian Batty

This is only a preview of the October 2023 issue of Practical Electronics.

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

Articles in this series:
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
Items relevant to "Stewart of Reading":
  • Bookshelf Speaker Passive Crossover PCB [01101201] (AUD $10.00)
  • Bookshelf Speaker Subwoofer Active Crossover PCB [01101202] (AUD $7.50)
  • Bookshelf Speaker Passive and Active Crossover PCB patterns (PDF download) [01101201-2] (Free)
  • Bookshelf Speaker System timber and metal cutting diagrams (PDF download) (Panel Artwork, Free)
Articles in this series:
  • Easy-to-build Bookshelf Speaker System (January 2020)
  • Building the new “bookshelf” stereo speakers, Pt 2 (February 2020)
  • Building Subwoofers for our new “Bookshelf” Speakers (March 2020)
  • Stewart of Reading (October 2023)
  • Stewart of Reading (November 2023)
  • ETI BUNDLE (December 2023)
  • Active Subwoofer For Hi-Fi at Home (January 2024)
  • Active Subwoofer For Hi-Fi at Home (February 2024)
Max’s Cool Beans By Max the Magnificent Arduino Bootcamp – Part 10 to fruition (a.k.a. winning the lottery), one of the first things I’m going to do is build myself a decent workshop. A few years ago, my chum Joe Farr, who lives in a little village outside London, created an air-conditioned construction attached to his house. When we are having a Zoom video call and Joe is showing me something running on the testbench, I can see his workspace with oscilloscope and logic analyser, soldering station, giant magnifying glass attached to his desk, and sundry other tools, all neatly arranged with lots of free space. Spools of wire of every gauge and colour you can imagine are mounted on the wall to the side of Joe’s desk, so all he needs to do is reach out, pull the required length, and snip it off. Another wall is covered floor-to-ceiling with little drawers packed with components beyond imagination. Most of the time, Joe can whip up a prototype of something we’ve been discussing using parts he already has to hand. As I pen these words, by comparison, I’m sitting hunched over the keyboard at my dining room table, which is buried under a mountain of breadboards, flying wires, microcontrollers, components and tools. If I put anything down on the table in front of me and turn my head, then when I look back just a second later… it’s gone. It’s like magic. All I can say is that as soon as I finish writing this column, my wife (Gina the Gorgeous) and I are going to hop in the car to go splash the cash on our weekly lottery ticket, thereby keeping the retirement dream alive. The knob on your side Older readers may remember The Goon Show, which was a British radio comedy program that was originally produced and broadcast by the BBC Home Service from 1951 to 1960 (it was a big favorite of King Charles III, as I recall). You can purchase a selection of scripts from amazon.co.uk if you wish (just search for ‘Goon Show Scripts’). I have them on the shelves in my office. I’m thinking of the episode in which one of the characters is trapped in a basement. Another character says, ‘Turn the knob on your side.’ The first guy responds, ‘I haven’t got a knob on my side,’ to which the second retorts, ‘Of the DOOR, you idiot!’ OK, you had to be there. But, speaking of knobs… It varies Thus far in this series of Arduino Bootcamp columns, we’ve used resistors with fixed values (Fig.1a). What we are going to do now is employ a variable resistor (Fig.1b). The way these little scamps usually work is by turning a knob (did you see what I just did there?) that slides a movable contact called the ‘wiper’ over a resistive element. The value associated with a variable resistor (10kΩ in this example) refers to the total resistance between one end of the resistive element and the other (between pins 1 and 3 in this example). Although variable resistors are conceptually simple, there are – as usual – a lot of fiddly details lurking ‘under the hood’ (or ‘under the bonnet’ in the English vernacular). First and foremost is the fact that there are three fundamental types of potentiometers, which are called linear (lin), logarithmic (log), and inverse logarithmic Resistive Wiper Knob element or anti-logarithmic (anti-log). Known as the resistance ‘taper’ 3 (or ‘curve’ or ‘law’), this refers 10kΩ 10kΩ 3 to the relationship between R1 V R1 2 2 the position of the wiper and 1 the value of the resistive ele1 ment (Fig.2). (a) Resis tor (b) Variable resistor (potentiometer) The most common type is a linear taper in which the Fig.1. Graphical and symbol representations of fixed resistance varies in a linear and variable resistors. fashion (in equal steps) in 42 concert with the wiper position. In the case of a logarithmic taper, the resistance varies logarithmically (in progressively larger steps) with the position of the wiper. The response of the human ear to the loudness of sound is logarithmic, thereby allowing us to hear both very quiet and very loud sounds. This explains why one common use of variable resistors with logarithmic tapers is to act as volume controls in audio equipment; also, why the logarithmic taper is known by some as the ‘audio taper.’ In rare cases, such as audio controls that are employed counterclockwise and in other specialised applications, anti-log versions are employed, so the anti-log taper may also be called the ‘reverse audio taper.’ Variable resistors manufactured in Asia and the US are usually marked with an ‘A’, ‘B’, or ‘C’ for log, lin, and anti-log tapers, respectively. Got potential? One thing that often baffles beginners to electronics is that we sometimes use multiple terms for the same thing. As we’ve discussed in earlier columns, for example, a standard resistor may also be referred to as a pull-up resistor, a pulldown resistor, a current-limiting resistor, and so on. It’s the same physical device type, but the qualification helps inform us what we are using it for. Although we would never do so (unless we were desperate), we could employ a variable resistor as a fixed resistor if we so-desired (Fig.3a). When we refer to this device as a ‘variable resistor’, we 100 Anti-Log 90 80 Resis tanc e value (%) I f my retirement plan ever comes 70 60 50 40 Lin 30 20 Log 10 0 0 10 20 30 40 50 60 70 80 90 100 Slider position (%) Fig.2. Different variable resistor tapers. Practical Electronics | October | 2023 V IN 3 3 1 3 2 2 3 2 Resis tanc e 1 1 2 1 Resis tanc e V OUT Voltage GND (a) Fixed resistor (b) Variable resistor (c) Potentiometer Fig.5. Breadboard-mounting trimpot. Fig.3. Variable resistor or potentiometer? are actually implying that we are using only two of its three terminals, one of which is connected to the wiper. Assuming the device is physically implemented as illustrated in our diagrams, and that we are using terminals 2 and 1 (Fig.3b), then rotating the knob clockwise will increase the resistance, while rotating the knob anticlockwise will decrease the resistance. Alternatively, if we were to use terminals 2 and 3, then the resistive response would be reversed. Last, but certainly not least, when a variable resistor is employed as a potential divider by using all three terminals (Fig.3c), then we refer to it as a ‘potentiometer’, or just ‘pot’ for short. Purely for the sake of interest, the potentiometer concept was first developed by the German physicist Johann Christian Poggendorff in 1841. The etymology of this word has its origin in 1868 as a hybrid formed from the combination of ‘potentio,’ derived from the Latin potentia, meaning ‘power’ (think potential), and ‘meter,’ from the Greek metron, meaning ‘measure.’ (I don’t know about you, but I love knowing where words come from). Mathematically speaking On the one hand, you don’t need to know what I’m about to tell you (at least, not for the purpose of our experiments here). On the other hand, I personally like to understand the nitty-gritty details of how things work. Consider a variable resistor being used as a potentiometer (Fig.4a). We can visualise the wiper as being attached to a tap point between two resistors (Fig.4b). I’m sad to say I don’t know the etymology of ‘tap’ in this context. However, I do know that it’s a ‘backronym’ (ie, an acronym invented after the fact) for ‘terminal access point’ in the context of networking applications. As we see, the output voltage (VOUT) is calculated as the input voltage (V IN) multiplied by (R1 / (R1 + R2)). If we assume that things are physically implemented as shown in our diagrams, and that the knob is rotated fully clockwise, then R1 will be 10kΩ and R2 will be 0Ω, which means (R1 / (R1 + R2)) will evaluate to 1, so VOUT = VIN. Contrariwise, if the knob is rotated fully anticlockwise, then R1 will be 0Ω and R2 will be 10kΩ, which means that (R1 / (R1 + R2)) will evaluate to 0, so VOUT = 0. Finally, suppose the wiper is in the middle, which means R1 and R2 will both be 5kΩ. In this case, (R1 / (R1 + R2)) will evaluate to 0.5, so VOUT will be half of VIN. V IN 10kΩ VR 1 R2 V OUT = ( R1 R1+ R2 ) × V R1 GND (a) Potentiometer Feeling trim? The term ‘trimmer potentiometer’ or ‘trimpot’ refers to a small potentiometer that is used to adjust, finetune, and calibrate circuits. Whereas regular potentiometers are typically mounted on the front panel of a piece of electronic equipment, trimpots are usually mounted directly on the printed circuit board (PCB). Generally speaking, the main difference between a pot and a trimpot is size and attachment location/mechanism. Having said this, many trimpots are created in such a way that they can only be adjusted by means of a small screwdriver or other tool. For the purposes of our experiment, we will be using a trimpot suitable for mounting on a breadboard (Fig.5). I purchased a 10-pack from Amazon in the US (https://bit.ly/3YErn5F), which is where I currently hang my hat. A similar pack is available from Amazon in the UK (https://bit.ly/3QAuz04). For myself, I like to have a few spares standing by, but we will require only one of these little rascals for our experiments, so you might be able to track down a one-off for a little less lucre. Say toodles to the trembler I don’t know about you, but my breadboard is becoming a tad crowded. I don’t think we will be using the trembler (vibration) switch we introduced in our previous column (PE, September 2023) for a while, so I’ve removed that from my breadboard and replaced it with one of my trusty trimpots, as illustrated in Fig.6. As usual, all of the files mentioned in this column are available from the October 2023 page of the PE website at: https://bit.ly/pe-downloads And, also as usual, you can download an image of our current breadboard layout showing the switches, our new 5V Pot (b) Equivalent circuit Fig.4. Calculating the resistance. Practical Electronics | October | 2023 10kΩ A0 A1 SW0 A2 SW1 GND IN To the Arduino’s A0 analogue pin GND 10kΩ 10kΩ Translucent view showing the pins underneath V IN V OUT You may be saying, ‘Well, that’s obvious,’ to yourself, but everything is once you know how to do it. All I know is that I would have really appreciated someone explaining this stuff to me when I was first starting out. To the Arduino’s A1 analogue pin New Existing momentary pushbutton switches potentiometer To the Arduino’s A2 analogue pin Fig.6. Adding the trimpot to the breadboard. 43 (a) Serial Monitor icon (b) Toggle Autoscroll icon (c) Example results (d) Aligned results Fig.7. IDE icons and example results. Listing 1. First-pass trimpot test. trimpot, our buzzer, and our 7-segment display – along with various pull-up and current-limiting resistors – coupled with the connections to our Arduino Uno (file CB-Oct23-01.pdf). The trimpot translucent view is intended to represent what we would see if we were looking down on a semi-transparent trimpot so we can visualise how the pins should be oriented underneath. For the devices we are using, we can rotate them by hand or by means of a small screwdriver inserted into the ‘arrow.’ The knob and shaft on our trimpot can only rotate through 270° (multiturn devices are available). With things as shown in Fig.6, the arrow is pointing at its halfway position. If we think of the arrow as being the hour hand on a clock, then when rotated fully anticlockwise or fully clockwise it will be pointing at approximately 7 o’clock and 5 o’clock, respectively. Work, does it? So, does our trimpot actually work? Let’s see, shall we? Enter the program shown in Listing 1 (file CB-Oct23-02.txt). On Line 1 we define a delay of 100 milliseconds (1/10 second). On Line 3 we declare a variable called PinPot, to which we assign the Arduino’s analogue input A2 (this is the pin we connected to the signal from our trimpot as shown in Fig.6). All we do in the setup() function is call Serial.begin() to activate the serial communications function, assign it the default speed of 9600 bits per second, and output a couple of lines of text to our Serial Monitor window. Listing 2. Just a little change. 44 The loop() function is just as simple. On Line 16 we declare an int (integer) variable called valPot (‘the value read from the potentiometer’) to which we assign the value returned from the analogRead() function. On Line 17 we use a Serial.print() statement to output some text to the Serial Monitor window without throwing a new line. On Line 18 we use a Serial.println() statement to output the current value in valPot while also throwing a new line. Last, but certainly not least, on Line 20 we pause for breath before doing it all again. Enter this program into your Arduino’s integrated development environment (IDE) and upload it into your Arduino Uno. Once the upload is complete, activate the Serial Monitor by clicking the appropriate icon in the upper-right-hand corner of the IDE (Fig.7a). After a couple of seconds, results will start to appear in the Serial Monitor (Fig.7c). Even though we set our PAUSE_BETWEEN_SAMPLES value to be 1/10 of a second, the values stream by at a high rate. One thing we can do is to click the Toggle Autoscroll icon in the upper-right-hand corner of the Serial Monitor window (Fig.7b). This stops the window scrolling, although new values continue to be appended to the bottom of the display. We can use the mouse to drag the scrollbar located at the right-hand side of the Serial Monitor window to wander back and forth through the results. Clicking the Toggle Autoscroll icon again will return the display to its automatic scrolling mode. Another thing that we might opt to do is change our PAUSE_BETWEEN_SAMPLES value to be something like 2000 (two seconds), which will slow things down nicely. Wait, what? Take another look at the results that are appearing on your screen and the example results in Fig.7c. What do these numbers actually mean? So, here’s the deal. The Arduino Uno has a 10-bit analogueto-digital converter (ADC). As we know, 10 bits can be used to represent 210 = 1024 different patterns of 0s and 1s, which we can use to represent unsigned (positive) integers in the range 0 to 1023. When we call our analogRead() function to read the voltage from our trimpot, we know that voltage can theoretically range between 0V and 5V. The ADC presents this voltage range to us as integer values from 0 to 1023 (if your trimpot only returns values slightly higher than 0 and slightly lower that 1023, this just means the wiper can’t quite reach the extents of its range). Are we aligned? Take yet one more look at the example results in Fig.7c. Is there anything about these results that irks you in any way? This raggedy presentation really gets my dander up. I prefer my numbers to be right aligned, especially when I have more than the single column we have here. Practical Electronics | October | 2023 Components from Part 1 LEDs (assorted colours) https://amzn.to/3E7VAQE Resistors (assorted values) https://amzn.to/3O4RvBt Solderless breadboard https://amzn.to/3O2L3e8 Multicore jumper wires (male-male) https://amzn.to/3O4hnxk Components from Part 2 7-segment display(s) https://amzn.to/3Afm8yu Components from Part 5 Momentary pushbutton switches https://amzn.to/3Tk7Q87 Components from Part 6 Passive piezoelectric buzzer https://amzn.to/3KmxjcX Components for Part 9 Listing 3. Reversing the trimpot’s action. SW-18010P vibration switch Let’s modify our loop() function as shown in Listing 2 (file CB-Oct23-03.txt). The changes in question appear in Lines 18, 19 and 20. If the value in valPot is less than 1000, we print a space. Similarly, if the value is less than 100 and less than 10, we print additional spaces. The result is an alignment that would bring a tear of joy to a Regimental Sergeant Major’s eye (Fig.7d). Components for Part 10 It’s ‘Backwards Day’ To be honest with you, I was sort of surprised that things worked out the way they did – that is, rotating the trimpot’s knob anticlockwise reduced the value being displayed, while rotating it clockwise increased the value being displayed. I don’t know why, but most of the time I do this, it ends up that my pot works backwards (the opposite way) to what I intended. Of course, it’s possible we actually wanted the pot to work the other way round such that rotating the knob anticlockwise increases the output value, and so forth. There are several ways to address this issue. We could fix it in hardware by swapping the power and ground connections to the trimpot. Alternatively, we could modify things in software by subtracting any values we read from the trimpot from 1023 as shown in Listing 3 (file CB-Oct23-04.txt). As we see, this required the addition of only a single statement on Line 17. The result is to ‘mirror’ the values, so 1023 minus a trimpot reading of 0 returns 1023, while 1023 minus a trimpot reading of 1023 returns 0, and so on for any intermediate values. Try entering this new version of the program into your Arduino and verify that it works as planned. Are you a convert? Just for giggles and grins, let’s say that, instead of displaying values in the range 0 to 1023, we wish to see these values represented as voltages between 0V and 5V rounded to the nearest volt. One way to do this is to use the Arduino’s built-in map() function, which re-maps a number from one range to another. This function requires five parameters: value (the variable to be processed), fromLow, fromHigh, toLow and toHigh. A value of fromLow gets mapped to toLow, a value of fromHigh to toHigh, and any values in-between to values in-between. In addition to implementing this new functionality, let’s also return to ‘Forwards Day’ by replacing the Line 17 we just added with our new map() statement, as illustrated in Listing 4 (file CB-Oct23-05.txt). This really is a pretty clever function because the ‘from’ range can be larger than the ‘to’ range (the way we are using it), or the ‘from’ range can be smaller than the ‘to’ range, in which case the function will perform linear interpolations as required. Also, the ‘low’ values can be smaller than the ‘high’ values (the way we are Practical Electronics | October | 2023 Breadboard mounting trimpots https://bit.ly/46SfDA4 https://bit.ly/3QAuz04 Listing 4. Mapping between number ranges. using it), or one or both ‘low’ values can be larger than their corresponding ‘high’ values, in which case the function will return just what you would expect it to (assuming you expect what it returns, of course). Time for homework Several potential tasks for you to play with popped into my mind while I was writing this column. Perhaps you could work on these to see how well you do, and we’ll compare results when next we meet: 1. Modify our latest program so it displays voltage values with two significant fractional digits, such as 0.00, 1.41, 3.30, 4.99, and 5.00. 2. Modify the program such that it only displays a new value in the Serial Monitor if the voltage changes by 0.01V or more. 3. Map the 0 to 1023 input values onto a range of 0 to 9 and, in addition to writing these values to the Serial Monitor, present them on our 7-segment display. 4. Augment the previous program to also cause our piezo-electric buzzer to play one of ten musical notes, with 0 corresponding to middle C (that’s C4 in scientific pitch notation). That should keep you busy for a while. Feel free to email me to tell me how you are getting on. Until next time, have a good one! 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 45