Silicon ChipMax’s Cool Beans - May 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: Spring is here at last
  7. Feature: Prophecy can be perplexing by Max the Magnificent
  8. Feature: The Fox Report by Barry Fox
  9. Feature: Net Work by Alan Winstanley
  10. Project: 500 WATTS POWER AMPLIFIER PART 2 by JOHN CLARKE
  11. Project: Precision AM-FM DDS Signal Generator by Charles Kosina
  12. Project: IMPROVED SMDTest Tweezers by Tim Blythman
  13. Feature: Make it with Micromite by Phil Boyce
  14. Feature: Max’s Cool Beans by Max the Magnificent
  15. Feature: Circuit Surgery by Ian Bell
  16. Feature: AUDIO OUT by Jake Rothman
  17. Feature: Electronic Building Blocks by Julian Edgar
  18. PCB Order Form
  19. Advertising Index

This is only a preview of the May 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)
Max’s Cool Beans By Max the Magnificent Arduino Bootcamp – Part 5 I ’m currently ambling around with a bounce in my stride and a great big sloppy grin slapped on my face. I just received an email that brought a flush to my cheeks (my face went red as well). The reason for my jaunty jocularity is that the communication in question came from Mike Cowlishaw, who is a visiting professor at the Department of Computer Science at the University of Warwick, a Fellow of the Royal Academy of Engineering, and a retired IBM Fellow, to name but a few of Cowlishaw’s cornucopia of credentials. Suffice it to say that Mike is something of a legend in certain circles (possibly triangles and squares too) – see: https://bit.ly/400Br8x In his communique, Mike spake as follows: ‘Hi Max, as usual, I thoroughly enjoyed your article in the April 2023 PE. Especially so because decimal representations and arithmetic are dear to my heart. I’ve been working on them, off and on, since 1981. I found out in 2001 that there was no standard way of doing decimal arithmetic. Different software and hardware could give different results for a multiply on the same arguments (even within the same company). However, I was able to fix that, and decimal arithmetic is now in the IEEE 754 standard for floating-point arithmetic, as well as in hardware. Here are a couple of references you might find interesting: Decimal Floating-Point: Algorism for Computers (some history and rationale, https://bit.ly/3JzMOz6), A Summary of Densely Packed Decimal encoding 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 (DPD) – more efficient than 8421 BCD; now implemented in hardware and software, https://bit.ly/3l3V1lq – and lots more at: https://bit.ly/3JaZwCF ’ Fig.2. Example 7-segment messages OH NO! a sort of graphical binary count, as illustrated in Fig.1. There are two questions you should be asking about Fig.1. First, why reference the six right-hand columns using the letters ‘A’ through ‘F’ rather than the numbers 10 through 15. Second, why highlight (in green) the two segments associated with the character in row 5, column 0. There are two answers with respect to the first question: (a) This figure looks crowded if we use the numbers 10 through 15 and (b) this leads us into the topic of hexadecimal numbers, which is introduced later in this column. In the case of the second question, I highlighted these two little rascals to illustrate the fact that some of our segment combinations may not appear to have any meaning when considered in isolation, but their significance becomes apparent when viewed in the context of other combinations. For example, these two segments are immediately apparent as representing a lowercase ‘r’ when seen in the context of the ‘Error’ message shown in Fig.2. Toward the end of my previous column (PE, April 2023), we noted that – since we can individually control the segments on our 7-segment display – we can use them to represent characters other than the numerals 0 through 9. As an example, we took the current latest and greatest version of our program, which repeatedly counts and displays the values 0 through 9, and we modified it to commence by first bidding us a cheery, ‘HELLO.’ We’re going to use this version of the program as the starting point for this month’s experiments. Just to ensure we’re all tapdancing to the same skirl of the bagpipes; you can download a copy of our current breadboard layout (file CB-May23-01.pdf) along with the latest version of our program (file CB-May23-02.txt). As usual, all the files mentioned in this column are available from the May 2023 page of the PE website at: https://bit.ly/pe-downloads Paste the program into your Arduino’s integrated development environment (IDE) and then save it with a name of your choosing along the lines of: Auto_Dec_Count_Up We closed the aforementioned column by asking you to try to think of other characters and symbols and messages we could create. Since we have seven segments (excluding the decimal point), and since each segment can be in one of two states (on and off), this means we have 27 = 128 different possibilities. The best way to start is to enumerate these possibilities using On Open Play Stop Off Close Pause Error Tick tock Although this is a bit of a (short) diversion, the topic of controlling individual segments on 7-segment displays just reminded me of the awesome project created by EE Student Frugha, as documented on Hackaday.io – see: https://bit.ly/3JxO8lS This little rascal (the project in Fig.3, not Frugha) involves a 6-row x 12-column 1 2 3 4 5 6 7 Fig.1. 16×8 grid showing the 128 possible states for a 7-segment display. Practical Electronics | May | 2023 Fig.3. One thing to do if you find yourself with 144 7-segment displays (Source: Frugha) 51 = 144 element array of 7-segment displays. Observe how some of the displays are presenting only fragments of characters. In addition to being employed as a clock, this bodacious beauty can also be used to display all sorts of other data and imagery. Hexactly Previously, we noted that groupings of four binary digits, or ‘bits’, are common. Such a grouping is known as a ‘nybble’ (or ‘nybl’ or ‘nibble’). Since four bits can represent 24 = 2 × 2 × 2 × 2 = 16 different combinations of 0s and 1s, we can use a nybble to encode (map) our ten decimal values as BCD. But this involves our employing only ten of the binary patterns, 0000 through 1001, leaving the remaining six patterns 1010 through 1111 unused and unloved (Fig.4a). Most of the time (ie, when we aren’t working in BCD), we prefer to take full advantage of all 16 binary patterns, 0000 through 1111, that are available in a nybble (Fig.4b). Although we can certainly consider these values in terms of their decimal equivalents (0 through 15), this quickly becomes clunky when we wish to work with multi-nybble values. Suppose we are working with a 4-nybble (16-bit) value, for example. If we consider this field to be in BCD format, we can use it to represent values between 0 and 9999. By comparison, if we consider it to be a standard binary field, it can support 216 = 65,536 different combinations of 0s and 1s, which we can use to represent unsigned (positive) values between 0 and 65,535. Unfortunately, large binary values typically don’t make much sense when we first see them. As an example, consider the 16-bit binary value 1010 0101 1110 1001 (the spaces have no meaning other than indicating that this is a B CD B in 23 22 21 20 23 22 21 20 Dec Hex 0 0000 1 2 0001 0010 0000 0 0 0001 0010 1 2 1 2 3 4 5 6 7 0011 0100 0011 0100 3 4 0101 0101 0110 0111 0110 0111 3 4 5 6 7 8 1000 1000 8 8 9 – – 1001 1010 1011 1001 1010 1011 9 10 11 9 A B – – 1100 1101 1100 1101 12 13 C D – – 1110 1111 1110 1111 14 15 E F (a) B CD (8421 F ormat) Unused Dec (b) B inary, Decimal, and Hexadecimal Fig.4. Introducing hexadecimal. 52 5 6 7 4-nybble value). This equates to 42,473 in decimal, but the mapping between the binary and decimal domains is not intuitively obvious. An alternative is to turn to the hexadecimal (base-16) number system. This requires us to assign unique symbols to each of the sixteen binary combinations supported by the nybble. We already have the 0 through 9 characters at our disposal, so we need only six more. Instead of inventing six completely new symbols – which would be a pain because (a) we would have to memorise them and (b) they wouldn’t exist on our computer keyboards – mathematicians and computer scientists decided to employ the letters A through F. Now let’s return to our previous 4-nybble example of 1010 0101 1110 1001. If we wish to transpose this into hexadecimal, all we need do is perform a one-for-one transformation between the four binary nybbles and their corresponding hexadecimal digits: 1010 = A, 0101 = 5, 1110 = E, and 1001 = 9. This means 1010 0101 1110 1001 in binary is the same as A5E9 in hexadecimal. Obviously, it’s just as easy to go in the other direction (we would be in a bit of a pickle were this not the case). If we start with a hexadecimal value of A5E9, then from Fig.4b we know that A = 1010, 5 = 0101, E = 1110, and 9 = 1001, so A5E9 in hexadecimal is the same as 1010 0101 1110 1001 in binary. As we’ve previously discussed, starting with the least-significant (rightmost) digit, column weights in binary (base-2) are 20 = 1, 21 = 2, 22 = 4, 23 = 8… and so on. By comparison, column weights in decimal (base-10) are 100 = 1, 101 = 10, 102 = 100, 103 = 1000… Thus, it should come as no surprise that the column weights in hexadecimal (base-16) are 160 = 1, 161 = 16, 162 = 256, 163 = 4096 and so on. Just for giggles and grins, let’s suppose we wish to convert our A5E9 hexadecimal value into decimal. As with any other place value number system, each digit forming the number is multiplied by its column’s weight and the results are summed to give the total value of that number. In this case, we have (A × 163) + (5 × 162) + (E × 161) + (9 × 160), which is the same as (A × 4096) + (5 × 256) + (E × 16) + (9 × 1), which is the same as (10 × 4096) + (5 × 256) + (14 × 16) + (9 × 1), which equals 42,473 in decimal (phew!). Last, but not least, when indicating hexadecimal values in the C/C++ programming languages, we prefix them with 0x or 0X (I prefer the former), for example, 0xA5E9. Also, we can use F E A G D lowercase letters if we wish, 0xa5e9, but I favour the uppercase approach. What? More characters? In our previous column, we decided which segments we were going to use to represent the decimal digits 0 through 9 on our 7-segment display (Fig.5). If we wish to display hexadecimal values on our 7-segment display, we are going to have to return to the drawing board to decide which segments we can use to represent the characters A through F. Sad to relate, although we can easily create uppercase representations of A, C, E, and F, we are obliged to make do with lowercase incarnations of ‘b’ and ‘d’ (Fig.6). The reason is simple: upper-case ‘B’ looks just like ‘8’ and upper-case ‘D’ could be mistaken for ‘0’ (zero). Furthermore, this explains why we use segment A in our representation of the number 6 because not using this segment in the letter ‘b’ allows us to differentiate between these two characters. Fire up the IDE! If you haven’t already done so, use the Arduino IDE to open our Auto_Dec_Count_Up program (or ‘sketch’ in the vernacular of the Arduino fraternity) and save it out as: Auto_Hex_Count_Up. Now, all we need to do is modify this program from repeatedly counting from 0 to 9 in decimal to repeatedly counting from 0 to F in hexadecimal. You’ve gone pale. What’s the problem? This really is easy peasy lemon squeezy because it involves only two small tweaks. First, we change our definition of NUM_ DIGITS from 10 to 16. Second, we modify our DigitSegs[] array to include the definitions of the segments used to represent the A through F characters, as illustrated in Listing 1. If you have any problems, you can download my version (file CB-May23-03.txt). Use the Sketch > Verify/Compile command to make our new program compile without errors, and then use the Sketch > Upload command to load the program into your Arduino and run it. Following the ‘HELLO’ message, you will (hopefully) see your 7-segment display repeatedly counting from 0 to F in hexadecimal. Switch it up! For our next experiment, rather than let our program count automatically, we’re going to control things by hand using a momentary pushbutton switch. The ‘momentary’ portion of this appellation means this is a non-latching switch that causes a temporary change in the state B C Fig.5. Segments used to represent 0 through 9. Fig.6. 7-segment A through F. Practical Electronics | May | 2023 colored pencil or a highlighter to mark Assuming you are using the things on the diagram as you verify them 2-pin version of the switch, on the breadboard. make sure the pins are presented as shown in Fig 7. Pressing the switch will make the Testing… testing… connection between the black Remember, we can make our lives a whole wire (connecting the left-hand lot easier if we take an ‘atomic’ view of the side of the switch to 0V) and world and test things in isolation before the blue wire (connecting the we use them as part of a larger function or right-hand side of the switch system. In this case, we will start by writto the ‘south’ side of the reing a simple program that reads the value sistor). It’s important to get of the switch and displays that value on the orientation of the switch our computer screen. Let’s call this procorrect. If you rotate it by 90°, gram something like Switch_Test_01. My the black and blue wires will version of this program is shown in Listremain forever unconnected ing 2 (file CB-May23-05.txt). no matter how enthusiastically We start by defining a constant called you press the button. SAMPLE_PERIOD and setting this to If you end up using the 100. Later, when we use this value in a 4-pin version of this switch, delay() function, it will be interpreted you’ll find the pins have a bit as 100ms (milliseconds), which means of a kink and you will have we will be reading (sampling) the state Listing 1. Modifying our DigitSegs[] array. to straighten them out using of our switch every tenth of a second. a pair of pliers. In this case, the pins are Next, we declare an integer variable of the circuit only while the switch is connected in pairs as illustrated in Fig.7 called PinSwitch and assign it a value activated (pressed). (use a multimeter to determine which pins of A0. In the real world, pin numbers These switches are available in 2-pin are connected if you’re not sure). Once depend on what type of processor we or 4-pin packages. Both types are 6mm again, it’s important to get the orientation are using. That’s why we inform the IDE square. Also, both are single-pole, sinof the switch correct. If you rotate it by of the processor type, which is the Argle-throw (SPST) devices with normally 90°, the result will be to permanently conduino Uno in our case. This allows the open (NO) contacts, which means pressnect the south side of the resistor to the IDE to automatically replace the A0 value ing the switch closes the contact and ground rail. This won’t do any damage, with the appropriate pin number when makes the connection. but the circuit will behave as though you we compile the program. Either type of switch will do, but I are constantly pressing the switch. As we see, the setup() function confavor the 2-pin versions for our breadIn the schematic (circuit) diagram portains only two statements. Let’s discuss board-based application. I purchased tion of Fig.7, we’ve used A0 to indicate these in reverse order. We use the ina pack of 20 for only $5.99 here in the the wire/signal feeding the Arduino’s A0 built pinMode() function to inform the US (https://amzn.to/3JG781t). The closinput. When the switch is open (inactive/ Arduino if we want its digital pins to act est equivalent I could find in the UK not pressed), we employ a pull-up resisas inputs or outputs. In earlier programs, was a pack of 20 switches for £3.99 tor to pull A0 up to 5V (logic 1 or HIGH). we’ve set the pins driving our 7-segment (https://amzn.to/3Tk7Q87). For some We can use any value between 1kΩ and display to be of type OUTPUT. In this case, reason, the UK versions have longer 10kΩ (or higher) for this resistor. Perwe are telling the Arduino that we want (14.5mm) leads, so you should cut sonally, I usually use 10kΩ values with the pin connected to our switch to be of these down to a suitable length for use their brown-black-orange bands for my type INPUT. If the truth be told, this is in your breadboard. pull-up and pull-down resistors. When somewhat redundant because – when it’s Thus far, we’ve been using the Arduthe switch is closed (active/ ino’s digital input/output (I/O) pins D0 pressed), it connects the A0 through D14. The Arduino Uno also has signal to 0V (logic 0 or LOW). six analogue input pins, A0 through A6. Once you’ve wired everyHappily, these analogue pins can also act thing up, STOP! This is the as digital pins if we wish, so let’s add one perfect time to slow down, of our switches along with a pull-up resistake a deep breath, and check tor to our breadboard and connect things that everything is as it should up as shown in Fig.7. An image of the be. Even though there are only entire breadboard showing this switch two components and three and the 7-segment display is available wires in this circuit, you’d in the download file CB-May23-04.pdf. be amazed how easy it is to do something silly, such as plugging the north end of the 5V resistor into the 0V (ground/ 4-P in 10K Ω blue) rail rather than the 5V (power/red) rail, for example. A0 2-P in (No, I certainly did not just do SW this myself. Why do you ask?) I recommend re-drawing G ND the circuit on paper, and then going through it componentT o the Arduino’s A0 analog pin by-component and connection-by-connection using a Listing 2. First switch test program. Fig.7. Adding a switch and pull-up resistor. Practical Electronics | May | 2023 53 powered-up – the Arduino will set all its pins to be inputs by default. However, defining this explicitly makes the program easier to read, so we do it anyway. Now let’s return to the first statement in our setup() function. The Arduino has a serial communications capability that allows it to talk to something called the ‘Serial Monitor’ on the host computer. Sometimes we may wish to use the Serial Monitor to send commands and data to the Arduino. In this case, however, we are going to use the Arduino to transmit information to be displayed on the Serial Monitor. The Serial.begin() statement tells the Arduino to get ready to exchange messages with the Serial Monitor. The argument of 9600 is commonly called the bit rate or the baud rate. This tells the Arduino we wish to use a data rate of 9600 bits per second (bps), which is the default used by the Serial Monitor (we could set this to be much higher if we wished). In the case of the loop() function, we start by declaring an integer variable named switchVal to which we assign the value returned when we call the built-in function digitalRead() to access the current state of our switch. As we’ve previously discussed, we can think of the values we read on our digital inputs as LOW (0V) or HIGH (5V), but the Arduino will see them in terms of 0 and 1, respectively. The Serial.print() statement prints whatever argument it is presented with. In this case, we are asking it to print the string ‘Switch = ’. This will leave the cursor on the same line on the screen, which means we can use multiple instantiations of this statement to build multielement lines on the display. Similarly, the Serial.println() statement will also print whatever argument it is presented with. In this case, we are asking it to print the value we read from our switch and loaded into our switchVal variable. However, this statement will also throw a new line character when it’s done, which will move the cursor to the next line on the display. Finally, we use the built-in delay() function to pause for SAMPLE_PERIOD (100 milliseconds), and then do the whole thing again. Upload this program into your Arduino and then use the Tools > Serial Monitor command to launch the Serial Monitor (or click the icon that looks like a magnifying glass with a dot in the center located in the upper right-hand corner of the IDE). After a second or so, you should see a never-ending series of the following lines of text start to appear in your Serial Monitor window: Switch = 1 Switch = 1 Switch = 1 : 54 If you see 0s instead of 1s, the first thing to check is that the north end of your resistor is connected to the 5V (red) rail and not the 0V (blue) rail by mistake. If you are using a 4-pin switch, the second thing to check is that it’s in the correct orientation (try rotating it clockwise by 90°). Once you start seeing the 1s, try pressing and holding the pushbutton, which should result in you seeing the following lines of text appear in your Serial Monitor window: Switch = 0 Switch = 0 Switch = 0 : Assuming you do see these 0s, play around by pressing and releasing the switch until your excitement is sated. However, if you continue to see 1s rather than 0s, the first thing to check is that the south side of your black wire is connected to the 0V (blue) rail and not the 5V (red) rail. If you are using a 2-pin switch, the second thing to check is that it’s in the correct orientation (try rotating it anticlockwise by 90°). Feeling bold? If you’re feeling bold, what I’d like you to do is return to our Auto_Dec_Count_Up program and save this as Switch_Test_02. Your mission, should you decide to accept it, is to use the minimum number of modifications necessary to tweak this program so that, after bidding us ‘HELLO,’ it loops around reading the state of the switch and displaying corresponding 0 and 1 values on the 7-segment display. Bolder still?? Serial functionality Much like the built-in functions pinMode(), digitalRead(), digitalWrite(), delay() and random(), the serial communications functionality presented here isn’t part of the C/C++ languages. In this case, it’s a library that’s been written in C++ and that’s automatically included in the Arduino’s IDE. Since this library is written in C++, the Serial portion of these statements is classed as being an ‘object,’ while the .begin(), .print() or .println() portions of the statements are officially referred to as ‘methods’ (supported operations of the object). Methods may also be referred to as ‘functions.’ More information about serial functionality is available in the reference section of the Arduino.cc website: https://bit.ly/2qOGWbo Yet bolder??? Wow! I have to say that I’m thrilled by your enthusiasm. In the case of this third undertaking, I want you to start by adding a second pushbutton switch (with associated pull-up resistor) to your breadboard and connect the output from this switch to pin A1 on your Arduino. Next, I want you to take the current version of your Switch_Dec_Count_Up program and save it as Switch_Dec_Count_ Up_and_Down. Modify this new incarnation of the program such that pressing the original switch causes the display to increment, while pressing the new switch causes it to decrement. In this case, if the display is currently at 0, then pressing the decrement button should cause it to wrap around back to 9. All of this should keep you busy for a while. I’ll present my versions of these programs in my next column. In the meantime, as always, I welcome your insightful comments, perspicacious questions, and sagacious suggestions. A second task I would be delighted for you to perform would be to return to our Auto_Dec_Count_Up program and save this as Switch_Dec_Count_Up. In this case, following the ‘HELLO’ message, we want to commence by presenting a 0 on the display. Next, we want the program to loop around checking the state of Components from Part 1 the switch. WhenLEDs (assorted colours) https://amzn.to/3E7VAQE ever the switch is Resistors (assorted values) https://amzn.to/3O4RvBt pressed, we want to Solderless breadboard https://amzn.to/3O2L3e8 increment (add 1 to) Multicore jumper wires (male-male) https://amzn.to/3O4hnxk the value on the display. As usual, when Components from Part 2 we reach 9, press7-segment display(s) https://amzn.to/3Afm8yu ing the button one more time will cause Components for Part 5 the display to wrap Momentary pushbutton switches https://amzn.to/3Tk7Q87 around back to 0. 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 Practical Electronics | May | 2023