Silicon ChipMax’s Cool Beans - February 2024 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions
  5. Back Issues: Hare & Forbes Machineryhouse
  6. Publisher's Letter: Teach-In 2024
  7. Feature: Cheeky chiplets by Max the Magnificent
  8. Feature: Net Work by Alan Winstanley
  9. Project: Active Mains Soft Starter by John Clarke
  10. Project: ADVANCED SMD TEST TWEEZERS by Tim Blythman
  11. Project: Active Subwoofer For Hi-Fi at Home by Phil Prosser
  12. Feature: Max’s Cool Beans by Max the Magnificent
  13. Feature: Circuit Surgery by Ian Bell
  14. Feature: MITCHELECTRONICS by Robin Mitchell
  15. Project: Audio Out by Jake Rothman
  16. PCB Order Form
  17. Advertising Index: Bush MB60 portable radio by Ian Batty

This is only a preview of the February 2024 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 "Active Mains Soft Starter":
  • Active Mains Soft Starter PCB [10110221] (AUD $10.00)
  • PIC12F617-I/P programmed for the Active Mains Soft Starter [1011022A.HEX] (Programmed Microcontroller, AUD $10.00)
  • Firmware for the Active Mains Soft Starter [1011022A] (Software, Free)
  • Active Mains Soft Starter PCB pattern (PDF download) [10110221] (Free)
  • Active Mains Soft Starter lid panel artwork (PDF download) (Free)
Articles in this series:
  • Active Mains Soft Starter, Part 1 (February 2023)
  • Active Mains Soft Starter, Part 2 (March 2023)
  • Active Mains Soft Starter (January 2024)
  • Active Mains Soft Starter (February 2024)
Items relevant to "ADVANCED SMD TEST TWEEZERS":
  • Advanced/ESR Test Tweezers back panel PCB (blue) [04105242] (AUD $2.50)
  • Advanced SMD Test Tweezers PCB set [04106221+04106212 {blue}] (AUD $10.00)
  • PIC24FJ256GA702-I/SS programmed for the Advanced SMD Test Tweezers (0410622A.HEX) (Programmed Microcontroller, AUD $15.00)
  • 0.96in cyan OLED with SSD1306 controller (Component, AUD $10.00)
  • Advanced SMD Test Tweezers kit (Component, AUD $45.00)
  • Firmware for the Advanced SMD Test Tweezers [0410622A.HEX] (Software, Free)
  • Advanced SMD Test Tweezers PCB patterns (PDF download) [04106221+04106212] (Free)
  • Advanced SMD Test Tweezers sticker artwork (PDF download) (Panel Artwork, Free)
Articles in this series:
  • Advanced Test Tweezers, Part 1 (February 2023)
  • Advanced Test Tweezers, Part 2 (March 2023)
  • ADVANCED SMD TEST TWEEZERS (January 2024)
  • ADVANCED SMD TEST TWEEZERS (February 2024)
Items relevant to "Active Subwoofer For Hi-Fi at Home":
  • 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)
Articles in this series:
  • Audio Out (January 2024)
  • Audio Out (February 2024)
  • AUDIO OUT (April 2024)
  • Audio Out (May 2024)
  • Audio Out (June 2024)
  • Audio Out (July 2024)
  • Audio Out (August 2024)
  • Audio Out (September 2024)
  • Audio Out (October 2024)
  • Audio Out (March 2025)
  • Audio Out (April 2025)
  • Audio Out (May 2025)
  • Audio Out (June 2025)
Max’s Cool Beans By Max the Magnificent Arduino Bootcamp – Part 14 W ell, hello there. May I be the first to say that you are looking particularly perspicacious and extraordinarily sagacious today. Now it’s your turn to say something nice about me… there’s no rush… I can wait… Crumbs! Thus far we’ve been performing all our experiments on a single breadboard. However, the time is drawing nigh (ie, in our next column) when we will need to use at least two of these little rascals, and it won’t take you long to conclude that the phrase ‘the more the merrier’ applies. Of course, the question you should be asking is, ‘If we won’t need multiple breadboards until next month, then why are you waffling on about them now?’ That’s a reasonable request and I’m glad you asked (although you didn’t need to be so snarky about it). The reason for my waffling is that I just heard from my old friend Joe Farr, who hails from England and who spake as follows: Max, long story short, I’m designing a large breadboard for use with op-amps, which means I need three power rails: positive, negative and 0V. I usually buy standard 840-hole breadboards and stick them to a base board to make a larger breadboard. I’ve got several of these larger breadboards, so I can have multiple projects on-thego at the same time. Breadboards are usually quite expensive, but I’ve just found these (https://bit.ly/46RMRi2) on CPC (part of Farnell, so a reputable company). They don’t have the coloured power and ground rail lines, which is perfect because I’m going to add my own markings. They are only £1.70 each (£2.04 inc. VAT). That’s peanuts. They are normally well over £7. The other thing that’s nice is it’s only a minimum £20 order for free delivery. I’ve just ordered 25 because they always come in handy, but the ones on eBay are all cheap imports and rubbish as the contacts deform as soon as you push a component lead in at the wrong angle. A few days later, Joe followed up to say that his breadboards had arrived. He went on to note: They are nice and tight and good lead grabbers that (so far) look like they will last, unlike the inferior foreign ones that are made of ‘tin’. He closed by saying, the power and ground rails are cut in the middle, so you need to add jumpers. As you may recall, I made mention of this type of split-rail board in my Arduinos and Solderless Breadboards blog: https://bit.ly/3NZ70uF The point is that this sort of deal won’t last forever, especially once the word gets out (shhh!), so I would advise Distance R Receive T Transm it HC-SR04 Object Fig.2. High-level, bird’s eye view of HC-SR04 operation. you to grab some of these bodacious beauties while the grabbing is good. Bouncy, bouncy! Much like the signals generated by the sensor we’re going to play with in this column, I’ve been bouncing off the walls with excitement at the thought of what is to come. As you may recall, we closed our previous column (PE, January 2024) cogitating on the concept of how a bat ‘sees’ the world when using ultrasonic sounds for echolocation. In our case, we’re going to use the HC-SR04 ultrasonic sensor (Fig.1). This little beauty – which can measure distances from around 2cm (approx 1 inch) to 400cm (say, 13 feet) with an accuracy of 3mm (approx 0.1 inch) – is available from multiple suppliers, including Adafruit via Amazon: https://bit.ly/49AMBq4 A high-level view of the way this works is illustrated in Fig.2. The pinouts and waveforms for the HC-SR04 are shown in Fig.3 and Fig.4, respectively. To be honest, I sort of made up the waveform traces shown in Fig.4 from bits and pieces I’ve gleaned on the interweb because I couldn’t find a single source that pulled everything together in a way that didn’t make me want to VCC TRIG ECHO GND S R 04 T R VCC GND TRIG ECHO Fig.1. The HC-SR04 ultrasonic sensor. Fig.3. HC-SR04 pinouts. 42 Practical Electronics | February | 2024 8 × 40kHz Pulses RX Pulse to Arduino Fig.4. HC-SR04 waveforms. gnash my teeth and rend my garb. I know we use the Arduino to apply a 10 microsecond (µs) positive-going pulse ( ) to the SR04’s TRIG (‘trigger’) input to set the ball rolling. Commencing on the falling edge of this pulse, the ultrasonic transmitter generates a series of eight 40kHz pulses. What is the duration of this series of pulses? I don’t know (sad face). I don’t believe it matters because of the way (I think) things work (happy face). The SR04’s ECHO output goes high at the end of the eight transmitted pulses. It stays high until the ultrasonic receiver ‘sees’ (‘hears’) the last of the eight reflected pulses. If there is no object in front of the SR04, and hence no reflected pulses, the SR04 will ‘time out’ and return the ECHO output to its logic 0 state after 38ms (milliseconds). A modicum of math In the case of sound passing through air, there are three interrelated parameters in which we are interested: distance, speed and time. Knowing any two of these parameters allows us to calculate the third. One way to visualise the relationship between these little scamps is by means of a handydandy magic triangle (Fig.5). So, assuming we are using our SR04 ultrasonic sensor to measure distance, we need to know the speed of our ultrasonic pulses in air and the time they take to reach an object and return. Do we actually know these values? Well… sort of. One thing we will know for sure is the duration of the pulse on the ECHO output. As we will see, this will be captured as an integer number of microseconds, but we’ll come back to that in a moment. How about the speed of sound in air? Well, one piece of good news is that this is not dependent on D S T D S T D S T Distance = Speed * Time Speed = Distance / Time Time = Distance / Speed Fig.5. Magic triangle for distance, speed and time. Practical Electronics | February | 2024 F A G Add one or both of these wires B E DP D C DIGITAL IN/OUT (PWM ~) Fig.6. Returning the ground wires. time (RTT) for the pulses to travel from the SR04 to the object and back again. Thus, we would divide our 42.875cm result by 2 to give 21.4375cm, which would be rounded to 21cm if we are storing this value using the int (integer) data type. Izzy wizzy, let’s get busy! Older readers may remember The Sooty Show, which was created by the English magician, puppeteer, and television presenter, Harry Corbett. This childhood favorite was produced for the BBC from 1955 to 1967, and then for ITV from 1968 until 1992. Harry was the presenter from 1955 until he passed in 1975, at which point his son, Matthew, took over the reins. The main characters (apart from the human presenter) were Sooty (a bear), Sweep (a dog), and Soo (a panda). Sooty, who was kind-hearted but also a bit cheeky (much like your humble narrator at that time), had a magic wand, which responded to the words ‘Izzy wizzy, let’s get busy!’ But we digress… First, we need to clear some space on our breadboard. We’re going to keep our 7-segment display and our piezoelectric buzzer along with any of their ancillary wires and components like current-limiting resistors. However, we’re going to remove our transistor, S R 04 T V CC T RI G E CH O G ND TX ECHO the frequency of the sounds in question, which means a dog’s bark, my squeals of delight, and ultrasonic pulses all travel at the same speed. More good news is that the speed of sound in air is not dependent on pressure (pressure and density both contribute to the speed of sound equally, but in opposition, and thus cancel each other out), which means my squawks of excitement will travel at the same speed, irrespective of whether I’m basking on a beach or moaning on a mountain. The bad news is that the speed in sound in air is affected by both temperature and humidity. If we assume dry air and a room temperature of 20°C (68°F), then the speed of sound is approximately 343 meters per second (m/s or ms–1) or 1,125 feet per second (ft/s). To put this in perspective, that means sound travels more than the length of three football fields every second. In a little while, we are going to use the Arduino’s built-in pulseIn() function to measure the duration of the pulse on the SR04’s ECHO output pin. This function accepts two arguments (well, it can accept three, but we are interested in only the first two for the purpose of these discussions): the number of the Arduino pin on which we want to read the pulse and the type of pulse we want to read – LOW (negative-going) ( ) or HIGH (positive-going) ( ). It returns a value in units of microseconds (µs), or 10-6 seconds. Now, when we are using a formula like distance = speed * time, all the units must be compatible. That is, we don’t want to be talking about distance in units of feet, speed in terms of furlongs per second, and time in units of jiffies in the same sentence (like wot I just did – oops!). Let’s start by assuming we wish to measure our distances in units of centimeters (cm). In this case, 343m/s = 34,300cm/s. Now let’s assume that we wish to employ time in units of µs. In this case, starting with 34,300cm/s and moving the decimal point six positions to the left gives us 0.0343cm/µs. As an example, suppose our SR04 presented us with a pulse on its ECHO pin of 1.25ms, which will be presented to us as 1,250µs from the Arduino’s pulseIn() function. Using distance = speed * time, this gives us a distance of 0.0343cm/µs * 1,250µs = 42.875cm. Would this mean the object was 42.875cm from the sensor? The short answer is ‘No!’ The longer answer is that, in this particular scenario, the value returned from the pulseIn() function is associated with the round-trip 10µs pulse from Arduino AREF GND 13 12 ~11 ~10 ~9 8 7 ~6 ~5 4 ~3 2 TX-1 RX-0 TRIG R To Arduino Pin 11 To Arduino Pin 12 Fig.7. Adding the SR04 to the breadboard. 43 Listing 2a. Setting-up the SR04. trimpot and light-dependent resistor (LDR) along with any of their ancillary wires and components. Next, we are going to add one or both of the original GND wires back to our 7-segment display (Fig.6). As you may recall, we removed these wires in our previous column (PE, January 2024) in order to add the transistor, which we used to control the brightness of the display. Since these wires connect to pins 3 and 8 of the display, and since they are connected inside the device, we really need only one of the wires. The reason for adding both is (a) we can and (b) to ‘play safe.’ Now it’s time to add our SR04 ultrasonic sensor to the breadboard. Observe that I’ve bent its pins back to make it point upwards from the breadboard (Fig.7). Just to ensure we’re all tap-dancing to the same skirl of the bagpipes, you can download an image of our entire current breadboard layout showing the SR04, piezoelectric buzzer and 7-segment display – along with any ancillary components – coupled with the connections to our Arduino Uno (file CB-Feb24-01.pdf). As usual, all the files mentioned in this column are available from the February 2024 page of the PE website: https://bit.ly/pe-downloads Are you ready to power-up this little beauty? Me too! Ping… ping… ping… The thought of a submarine just popped into my head accompanied by the ‘ping… ping… ping…’ sound of its sonar, which stands for ‘sound navigation ranging.’ More generally, according to Wikipedia, ‘Sonar is a technique that uses sound propagation to navigate, measure distances, and communicate with or detect objects on or under the surface of the water.’ When you think about it, we are doing something similar in air by implementing a form of acoustic radar, or ‘sodar’, which stands for ‘sonic detection and ranging.’ As usual, we are going to take things step by step. We will commence by ensuring that our ultrasonic sensor is functioning as we expect. We’ve already wired everything up, so now it’s time to turn our attention to the software. We will start with a simple program file: CB-Feb24-02.txt Since our first program is so small, it’s worth considering it in its entirety. We’ll start by setting everything up 44 Listing 2b. Using the SR04. as shown in Listing 2a. (Remember that, following some confusion in earlier columns, we’re now using a scheme in which the listing number [Listing 2 in this example] corresponds to the associated program file (CB-Feb24-02.txt in this example), after which we use ‘a’, ‘b’, ‘c’ … suffixes as appropriate.) I’m sitting at my desk with my breadboard in front of me and my SR04 pointing toward the ceiling. I’m planning on using the SR04 to measure the height of my hand above the sensor. Since I’m lazy and have no intention of standing up, on Line 1 I’ve defined MAX_DIST as being 100, which is the maximum distance I’m interested in measuring specified in centimeters. On Lines 3 and 4 we declare two integers, PinTrig and PinEcho, to which we assign the numbers of the Arduino pins we’ve connected to the SR04’s TRIG and ECHO terminals, respectively. Next, in the setup() function, we initialise our serial communications (Line 10), define the modes for our pins (Lines 14 and 15), tell the PinTrig pin to output a LOW (logic 0) value (Line 16), and write a few lines of text to the Serial Monitor (Lines 18 and 19). The loop() function, which is shown in Listing 2b, is where things start to get interesting. We start by declaring two variables on Lines 26 and 27: an int (integer) called distance and a long int (long integer) called duration. We use a long int for duration because this data type can store larger values than a regular int (we introduced the concepts of int, short int and long int in PE, September 2023). On Lines 30, 31 and 32 we output a 10µs positive-going pulse on PinTrig to kick everything off. On Line 35 we use the Arduino’s built-in pulseIn() function to read the width of the pulse generated by the SR04 on its ECHO pin, Practical Electronics | February | 2024 which we’ve connected to PinEcho on the Arduino. On Line 38 we use the equation we discussed in excruciating detail earlier to calculate the distance to any object in centimeters. Finally, on lines 41 through 51 we display the result on the Serial Monitor, after which we return to the start of the loop() function and do it all again. When you first run this program and open the Serial Monitor on your host computer, you should see the following: ============== Distance = --Distance = --Distance = --: If you now place your hand in the air over the sensor and move it up and down, you’ll start to see values like the following: Distance = --Distance = --Distance = 23 Distance = 24 Distance = 23 Distance = 22 : If you see only 0 values, check that you’ve got the wires linking the SR04 to the Arduino plugged into the correct locations at each end and that you’ve used the right pin numbers in your code (no, of course I’m not speaking from experience – why would you ask?). I’ve said it before, and I’ll doubtless say it again – even though this is all simple stuff in the grand scheme of things, I still get a real thrill when (a) I implement the hardware and software to do something like use an ultrasonic sensor to detect and report the motions of my hand waving in the air and (b) everything actually works as planned! Adding the 7-segment display We are currently in the enviable position that we are no longer obliged to create everything from scratch. Instead, we can use bits and pieces of our previous programs like LEGO building blocks to create new implementations. That’s what I’ve just done. I took the code we just created and merged it with an earlier program that wrote the decimal digits 0 through 9 to the display. I invite you to feast your orbs on the result (file CB-Feb24-03.txt). We won’t go through the entire program here, but there are a few points worth noting. For example, we’ve reduced MAX_DIST (the maximum distance we wish to measure) from 100cm to 99cm because we wish (it’s easier) to display only two digits that can range between 00 and 99. Hmmm. Two digits. The problem is that we currently have only one 7-segment display. Of course, one way around this would be to add more 7-segment displays, which is what we will be doing in future columns. The alternative is to use our single display to present both digits. If we call our most-significant and least-significant digits MSD and LSD, respectively, and if we think of ‘…’ as indicating a pause, then a first-pass view would be to display MSD … LSD … … … MSD … LSD … … …, and so forth. We’ve also got to consider the case where the distance read from the SR04 is greater than 99. In our previous program, we simply wrote hyphen (‘-’) characters to the Serial Monitor. We are going to do the same thing with our 7-segment display, which is why we’ve defined HYPHEN_SEG as being B00000010, where the ‘1’ equates to the horizontal ‘G’ segment in the middle of the display. Three more new definitions are as follows: ON_TIME (initially set to 200ms), which is the time a digit will be displayed; SML_PAUSE (‘small pause,’ initially set to 100ms), which is the time we will turn the display off between the digits forming a pair; and BIG_PAUSE (initially set to 700ms), which is the time we will turn the display off between pairs of digits. Most of the program will be familiar from our previous experiments. The only new part is where we take the distance read from the SR04 and present it on the 7-segment display as illustrated in Listing 3a. At the start of our loop() function (not shown here), we’ve declared two variables of type byte that we’ve called msdSegs and lsdSegs. We will use these variables to hold the segments associated with the MSD and LSD, respectively. On Line 93 we perform a test to see if the distance measured by the SR04 is greater than 99cm. If so, on Lines 95 and 96, we assign the bit pattern associated with our hyphen segment to both msdSegs and lsdSegs. Otherwise, on Lines 100 and 101, we assign the bit patterns associated with most- and least-significant digits to msdSegs and lsdSegs, respectively. How does this work? Well, suppose our distance measurement is 42. Since we are working with integers, the division operation 42 / 10 will return a value of 4, which Online resources Listing 3a. Displaying 2-digit distance on the 7-segment display. Practical Electronics | February | 2024 For the purposes of this series, I’m going to assume that you are already familiar with fundamental concepts like voltage, current and resistance. If not, you might want to start by perusing and pondering a short series of articles I penned on these very topics – see: https://bit.ly/3EguiJh Similarly, I’ll assume you are no stranger to solderless breadboards. Having said this, even if you’ve used these little scamps before, there are some aspects to them that can trap the unwary, so may I suggest you feast your orbs on a column I wrote just for you – see: https://bit.ly/3NZ70uF Last, but not least, you will find a treasure trove of resources at the Arduino.cc website, including example programs and reference documentation. 45 Listing 4a. The heart of the therebone. is our most-significant digit, while the modulus operation 42 % 10 will return a value of 2 (the remainder from the division), which is our least-significant digit. Lines 104 through 107 display the most-significant digit for a duration of ON_TIME followed by a short blanking period with a duration of SML_PAUSE. Lines 109 through 112 display the least-significant digit for a duration of ON_TIME followed by a longer blanking period with a duration of BIG_PAUSE. I can’t stop myself from chortling as I move my hand up and down and see the result presented on both the Serial Monitor and on my 7-segment display. Even my wife (Gina the Gorgeous) was impressed, and that’s not something I expect to hear myself say very often! Sound the therebone! As we’ve previously discussed, an unexpected entry to the weird and wonderful musical instruments of the 20th Century was the theremin (https://bit.ly/3ubLrBj). This eldritch-sounding gizmo was named after its inventor, the Russian physicist Lev Sergeyevich Termen, who is known in the West as Leon Theremin. Invented in 1919 and patented in 1928, the theremin was the product of Soviet government-sponsored research into proximity sensors. Now, by (total lack of) popular request, we dare to present our 21st Century rebuttal instrument in the form of the soon-to-be-legendary therebone™, whose moniker is derived from the fact that I used to play the trombone in the Sheffield Schools Orchestra deep in the mists of time. I just forked our previous program to create a new version in which we use the distance measured using our ultrasonic range finder to control the frequency of notes played on our piezoelectric buzzer, file CB-Feb24-04.txt. In our previous program, we decided to measure distance in units of centimeters. In the case of our new program, I’ve decided to use units of millimeters. Assuming the speed of sound to be 343m/s, this equates to 343,000mm/s. And, since we’re measuring time in units of µs, this equates to 0.343mm/µs, which is the value we will use in our equation. We start with four definitions: MIN_DIST (‘minimum distance,’ which we set to 20mm), MAX_DIST (‘maximum distance,’ which we set to 999mm), MIN_FREQ (‘minimum frequency,’ which we set to 31Hz), and MAX_FREQ (‘maximum frequency,’ which we set to 4,978Hz). The 31Hz and 4,978Hz values correspond to B0 and DS8 in scientific pitch notation (SPN), also known as American standard pitch notation (ASPN) and international pitch notation (IPN). We also define a SAMPLE_TIME as being 40ms. I’ll explain why we need this in a moment. We use only three of the Arduino’s pins in this program: pin 12 (PinTrig) and pin 11 (PinEcho) for the SR04, and pin 10 (PinBz) to drive our piezo buzzer. Most of the program is self-explanatory, but it’s worth our while to consider the contents of the loop() function, as illustrated in Listing 4a. The first point to note is Line 42, where we use our new value of 0.343mm/µs to calculate distance in millimeters. Remembering that we are going to use our hand to control the frequency of a musical note, one point to consider is what we are going to do if our hand moves outside the permitted range. Initially, I was toying with the idea of using the Arduino’s built-in constrain() function to constrain our distance value to always fall between MIN_DIST and MAX_DIST. The way I would have done this would be to add the following statement to Line 43: distance = constrain(distance, MIN_DIST, MAX_DIST); A more generic representation is as follows: r = constrain(x, a, b); 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 46 This will return x if x is between a and b; it will return a if x is less than a; and it will return b if x is greater than b. Practical Electronics | February | 2024 But then I realised that, if I wasn’t careful, removing my hand from being in front of the SR04 would leave the current note playing ad infinitum. So, instead of using the constrain() function, I added the if() test on Line 45. If the measured distance falls in the allowable range, then we call the Arduino’s map() function on Line 48 to map the distance in mm to a corresponding frequency in Hz (we introduced this function in PE, October 2023), after which we call the tone() function on Line 51 to play the desired note (we introduced this function in PE, December 2023). As soon as the measured distance falls outside the allowable range – such as when I remove my hand from being in front of the SR04 – then we call the noTone() function on Line 55 to turn off the currently playing note. When I ran the first iteration of this program, the sound was horrible indescribable. I quickly realised that I was repeatedly calling the tone() function so quickly that it didn’t have time to actually do anything worth doing. This is why I added the delay on Line 59, after which the sound was… everything we might expect. All joking aside, this actually sounds pretty good considering the parts and techniques we are using. The quality of the sound could be dramatically improved by replacing the piezo buzzer with a regular loudspeaker and by adding some analogue signal processing (ASP) circuitry. All-in-all, however, I’m very pleased with the result. A theremin has two controls – one for pitch and one for volume. Based on this, one future therebone enhancement might be to add a second SR04, thereby allowing the therebone artist to control the pitch with one hand and the volume with another. Although my dad was English, he was a member of the 15th Scottish Reconnaissance Regiment during WWII. Dad told me that the regimental pipers played their great Highland bagpipes through the night before the company went into battle. The idea was to plant fear, uncertainty, and doubt (FUD) into the minds of the enemy. Dad said that he didn’t know what effect it had on the Germans, but after a night of listening to the bagpipes, he was prepared to fight anyone. We can only wonder what he would have thought about the therebone. Broken promises In my previous column, I said that one of the things we were going to do in this column was introduce the concept of realtime clocks (RTCs). Paradoxically, we don’t have enough time, so we will have to punt those discussions to our next column. Until then, as always, have a good one! NEW! 5-year collection 2017-2021 All 60 issues from Jan 2017 to Dec 2021 for just £44.95 PDF files ready for immediate download See page 6 for further details and other great back-issue offers. Purchase and download at: www.electronpublishing.com 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 JTAG Connector Plugs Directly into PCB!! No Header! No Brainer! Components from Part 6 Passive piezoelectric buzzer https://amzn.to/3KmxjcX Components for Part 9 SW-18010P vibration switch https://bit.ly/46SfDA4 Components for Part 10 Breadboard mounting trimpots https://bit.ly/3QAuz04 Components for Part 12 Light-Dependent Resistor https://bit.ly/3S2430m Components for Part 13 BC337 NPN Transistor https://bit.ly/40xAgyS Components for Part 14 HC-SR04 Ultrasonic Sensor https://bit.ly/49AMBq4 Practical Electronics | February | 2024 Our patented range of Plug-of-Nails™ spring-pin cables plug directly into a tiny footprint of pads and locating holes in your PCB, eliminating the need for a mating header. Save Cost & Space on Every PCB!! Solutions for: PIC . dsPIC . ARM . MSP430 . Atmel . Generic JTAG . Altera Xilinx . BDM . C2000 . SPY-BI-WIRE . SPI / IIC . Altium Mini-HDMI . & More www.PlugOfNails.com Tag-Connector footprints as small as 0.02 sq. inch (0.13 sq cm) 47