Silicon ChipArduino Bootcamp – Part 12 - December 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
  7. Feature: Good grief! by Max the Magnificent
  8. Feature: Net Work by Alan Winstanley
  9. Feature: The Fox Report by Barry Fox
  10. Project: Digital Boost Regulator by Tim Blythman
  11. Project: Dual-Channel Power Supply for BREADBOARDS by Tim Blythman
  12. Project: Display Adaptor for the BREADBOARD PSU by Tim Blythman
  13. Feature: Arduino Bootcamp – Part 12 by Max’s Cool Beans
  14. Feature: Circuit Surgery by Ian Bell
  15. Project: The 555 Timer IC – Part 1 by Robin Mitchell
  16. Feature: KickStart by Mike Tooley
  17. PCB Order Form
  18. Advertising Index by Ian Batty

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

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

Articles in this series:
  • (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)
Items relevant to "ETI BUNDLE":
  • 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)
  • Easy-to-build Bookshelf Speaker System (January 2020)
  • Building the new “bookshelf” stereo speakers, Pt 2 (February 2020)
  • Building the new “bookshelf” stereo speakers, Pt 2 (February 2020)
  • Building Subwoofers for our new “Bookshelf” Speakers (March 2020)
  • Building Subwoofers for our new “Bookshelf” Speakers (March 2020)
  • Stewart of Reading (October 2023)
  • Stewart of Reading (October 2023)
  • Stewart of Reading (November 2023)
  • Stewart of Reading (November 2023)
  • ETI BUNDLE (December 2023)
  • ETI BUNDLE (December 2023)
  • Active Subwoofer For Hi-Fi at Home (January 2024)
  • Active Subwoofer For Hi-Fi at Home (January 2024)
  • Active Subwoofer For Hi-Fi at Home (February 2024)
  • Active Subwoofer For Hi-Fi at Home (February 2024)
Max’s Cool Beans By Max the Magnificent Arduino Bootcamp – Part 12 I don’t know about you, but our previous column (PE, November 2023) already seems like a lifetime away. Just to remind ourselves as to where we’re at, we used the value read from our trimpot to control the frequency of segment D flashing on our 7-segment display. Next, for giggles and grins, we used the trimpot to control the brightness of segment D. As usual, you can download an image of our current breadboard layout showing the switches, our trimpot, our piezoelectric 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-Dec23-01.pdf). Also, as usual, all the files mentioned in this column are available from the December 2023 page of the PE website: https://bit.ly/pe-downloads It hurts me to say… This is going to hurt me more than it hurts you because I’m the one who is going to do all the work. As you may recall, we still have two of our homework assignments outstanding from our last-but-one column (PE, October 2023). The first is to map the 0 to 1023 input values read from our trimpot 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. The great thing about this is that we’ve already created a bunch of little programs. What I’m going to do is root around our earlier offerings to find one that displays the digits 0 through 9. I’m also going to take one of the examples from our previous issue – the one that reads the 0 to 1023 values from our trimpot and uses our own mapping function to translate them into a 0 to 9 subset. I’m going to munge these two programs together, mix in a little magic, and see what happens. So, make yourself comfortable. I’ll be back in just a moment. There, that didn’t take long, did it? You will be relieved to hear that this took only a couple of minutes and it works like a charm. You can download the resulting program (file CB-Dec23-02.txt) to peruse, ponder, and play with it at your leisure. There’s no need for us to show the whole program here. Suffice it to say that we start by defining a few useful values. Next, we declare a global integer called PinPot to which we assign the number of the analog input pin that’s connected to our trimpot. We also declare a global array of integers called PinsSegs[] to which we assign the numbers of the eight digital pins we wish to use as outputs to drive our 7-segment display (remember that there’s a decimal point segment in addition to the seven A through G segments). And, of course, we declare a global array of 8-bit bytes called DigitSegs[] to define which segments on the 7-segment display correspond to the decimal digits we wish to present. One more thing while I’m thinking about it is that we declare a global integer called OldPot (‘old value from the trimpot’) to which we assign a value of –1 (we use –1 as an initial value because this isn’t something we will ever see from our trimpot). 42 Listing 2a. Our new loop() function. In addition to the setup() and loop() functions, we have only two of our own functions. The first, MyMap(), is the one we created in our previous column to map the 0 to 1023 values from the trimpot to the desired 0 to 9 subset. The second, DisplaySegs(), is our old friend that we use to turn the various display segments on and off to represent the decimal digits 0 through 9. The main function of interest to us here is loop(), as illustrated in Listing 2a. (Following some confusion associated with my previous column, we’re using a new scheme in which the listing number (Listing 2 in this example) corresponds to the associated program file (CB-Dec23-02.txt in this example), and then we’ll use ‘a’, ‘b’, ‘c’… as suffixes, as appropriate). We start on Line 51 by declaring a local integer variable called valPot (‘value from the trimpot’). On Line 52 we declare a local 8-bit byte variable called charSegs (‘character segments’). On Line 54 we read a value of 0 to 1023 from the trimpot and load it into valPot. On Line 55 we call our MyMap() function to convert this value into our 0 to 9 subset. We perform a test on Line 57 to see if this new value is different (not equal) to the old one (this test will definitely return true the first time we pass through the loop because of the –1 value with which we initialised OldPot). If so, we execute the statements in the block defined by the { and } curly brackets on Lines 58 and 66. First, on Line 59, we copy the new local mapped value from the trimpot into the old global value so we’ll remember it the next time we pass through the loop. Second, on Lines 61 and 62, we output the Practical Electronics | December | 2023 trimpot value to the Serial Monitor. And third, on Lines 64 and 65, we light up the appropriate segments on our 7-segment display. You may be wondering about the call to the delay() function on Line 68 (I’ve defined SAMPLE_TIME as being 100 milliseconds (ms), or 1/10th of a second). On the other hand, you may not have even noticed it. Whichever, now that I’ve brought it to your attention, why do you think I included this little scamp? Well, the thing is, even though the Arduino Uno’s clock is running at only 16MHz, which is slow for a microcontroller these days, that’s still 16 million cycles per second. What this means is that, if left unchecked, the Uno will sample the value from the trimpot multiple times as we cross the boundary equating to one of our 0 to 9 mapped values to another. This wouldn’t be a problem if valPot transitioned smoothly from one value to another, but it typically doesn’t because the values being read from the trimpot are inherently noisy. As a result, rather than our mapped value transitioning cleanly from …5 5 5 to 6 6 6…, for example, what we will usually see is a transition more along the lines of …5 5 5 6 5 6 5 6 6 6… which is a tad annoying. You can watch it happening on your own rig by deleting Line 68 or by changing the value associated with SAMPLE_TIME to 0. The bottom line is that including a 100ms sample time gives the trimpot – and hence the value on valPot – time to firmly transition from one value to the next. I don’t know about you, but as I’ve mentioned before, even though this is really very simple stuff, I still get a buzz of excitement when I rotate my trimpot and I see my 7-segment display respond. You hum it son… Do you remember the TV adverts for PG Tips tea bags featuring anthropomorphic chimpanzees wearing human clothes with dubbed voices provided by celebrities, such as Peter Sellers, Donald Sinden and Bob Monkhouse? One of these adverts featured two of the chimpanzees moving a piano. The younger one says, ‘Dad, do you know the piano’s on my foot?’ The father replies, ‘You hum it son, and I’ll play it!’ All of which serves as smooth a segue as you might wish into our final homework assignment, which was to augment the program we just created 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). How are we going to do this? Well, a good starting point is to decide which notes we wish to play. If we are going to exclude sharps and flats, then our 0 to 9 values will correspond to C4, D4, E4, F4, G4, A4, B4, C5, D5, and E5, respectively. Alternatively, if we decide to include sharps and flats, then our 0 to 9 values will correspond to C4, CS4, D4, DS4, E4, F4, FS4, G4, GS4, and A4, respectively, where the ‘S’ characters stand for ‘sharp,’ and F# (F sharp) is the same as G♭ Listing 3a. Array of note frequencies. Practical Electronics | December | 2023 (G flat), for example. We need to pick one or the other, so I’m going to adopt the former scheme. The next step is to determine which frequencies are associated with each of the notes. For this, I turned to a superuseful book called Exploring Arduino, Second Edition, by Jeremy Blum, see: https://bit.ly/3LYAbOJ For the purposes of this column, Chapter 6: Making Sounds and Music, is of particular interest, not least because of the code examples we can download from his Exploring Arduino website, especially the page for Chapter 6 – see: https://bit.ly/3PXTtEY Once you have safely arrived there, click the ‘Download Code’ button, and you are presented with a compressed ZIP file. One of the files contained within is called pitches.h (I’ve provided a text version for your delectation and delight – file pitches.txt – which you can rename to pitches.h and then include in your programs as a header file if you wish, but we aren’t going to do that here). From this file, we learn that the frequencies associated with our chosen set of notes are as follows: C4 D4 E4 F4 G4 A4 B4 C5 D5 E5 262Hz 294Hz 330Hz 349Hz 392Hz 440Hz 494Hz 523Hz 587Hz 659Hz As an aside, C or ‘Do’ (as in Do Ray Me Fa So La Ti Do) is the first note and semitone of the C major scale Do you remember the classic flashmob singing of the Do Ray Me song in Antwerp train station? – see: https://bit.ly/3PWYhdN It’s interesting to note that the actual frequency of C4 has depended on historical pitch standards. According to Wikipedia, for example, ‘For an instrument in equal temperament tuned to the A440 pitch standard widely adopted in 1939, middle C has a frequency around 261.63Hz.’ As we see, Jeremy has rounded his values to the nearest integer. Also of interest is the fact that, for some purposes, a convention may be adopted in which the frequencies of the various Cs are powers of two. In this case, the frequency of middle C would be said to be 256Hz. But we digress… Take a note For some reason I’m envisaging the Welsh prop comedian and magician Tommy Cooper saying, ‘Take a note... any note… but don’t let me see which one.’ I’m also remembering the English comedian Eric Morcombe saying to his sidekick Ernie Wise, ‘I am playing all of the notes… just not necessarily in the right order is all.’ In the context of software development, the term ‘fork’ refers to taking the source code for an existing project and using it to create a new application based on the original code. So, to add sounds to our code, we’re going to fork the program we created earlier, after which we will edit the forked version (file CB-Dec23-03.txt). The first thing we will do, for reasons that will become apparent, is to change the value associated with SAMPLE_TIME to 250 (that is 250ms, 1/4 of a second). Next, we will declare a global integer called PinBz, to which we assign the number of the digital pin we are using to drive our piezoelectric buzzer. As part of this, we will add a pinMode() statement to our setup() function to inform our Arduino Uno that we want to use this pin as an output. Also, we will declare a global array of integers called OurNotes[] containing the ten frequency values associated 43 Listing 3b. First modification to loop(). with the notes C4 through E5. Noting that NUM_DIGITS has been defined as 10, this array is shown in Listing 3a. Happily for us, the Arduino supports a built-in function called tone() that we can use to play notes using our piezo buzzer. There’s also a corresponding noTone() function. There are two ways to call the tone() function and one way to call the noTone() function as follows: tone(pin, frequency); tone(pin, frequency, duration); noTone(pin); The pin is the Arduino pin on which we wish to generate the tone (the pin connected to our piezo buzzer in our case). The frequency is the frequency of the desired tone in hertz. The duration is the duration of the tone in milliseconds. If this last argument is omitted, the note will continue to play until a new call to the tone() function changes the frequency or a call to the noTone() function terminates the note. There are various considerations involved with the tone() function, such as the fact that it will interfere with the Arduino Uno’s pulse-width modulated (PWM) outputs on pins 3 and 11, so I strongly recommend you take a look at the Arduino Reference Guide for this function before you use it for anything other than the examples shown here – see: https://bit.ly/46wGvoQ Last, but not least, we need to modify our loop() function as shown in Listing 3b. As we see, all we need do is add a call to the tone() function on Line 77, employing the same valPot value we are using to control the 7-segment display to index into our OurNotes[] array of frequencies. I just tried running my original version of this program. Unfortunately, the constantly playing tones acted like a siren song to our two stupid cats, who came to see what was happening and subsequently attempted to rearrange the components on my breadboard. I could have added a duration argument to the call to the tone() function. As an alternative, just to show this in action, I added a call to the noTone() function on Line 87 of the loop() function, as shown in Listing 4a (file CB-Dec23-04.txt). 44 Listing 4a. Second modification to loop(). This is why we increased the SAMPLE_TIME value to 250 earlier. Now, when we rotate our trimpot, every time we transition to a new value, we receive a quarter-second note followed by glorious cat-free silence. Reinventing the wheel In a moment we are going to be talking about power, so let’s briefly refresh our memories. One equation of interest here is P = V × I, where P is power (measured in watts), V is voltage (measured in volts), and I is current (measured in amps). Also, from Ohm’s law (which we introduced in PE, January 2023), we know that V = I × R, where R is resistance measured in ohms (symbol Ω). This means we can quickly calculate P, V, I, and R so long as at least two of the values are known. There’s a very useful Power P V2 / R Voltage P /I V×I I2 × R P Watts I P /R Amps V V olts P×R R V2 / P Ohms P / I2 P /V I V I×R V /R Current V /I R Resistance Fig.1. Ohm’s law wheel. Practical Electronics | December | 2023 visual representation of this called Ohm’s wheel or Ohm’s law wheel (Fig.1). Anything but futile If you’re a fan of Star Trek, you’ll understand the phrase ‘Resistance is Futile’ to mean ‘Don’t bother resisting because we’ll overtake you.’ This was the way the Borg – cybernetic organisms linked in a hive mind – preferred to say ‘Hello.’ Of course, it didn’t take long before electronics engineers started sporting T-Shirts with a resistor symbol accompanied by the text ‘IS FUTILE’ (to know us is to love us). In reality, of course, resistance is anything but futile because it can be used for all sorts of things, including sensors whose resistive values vary as a function of whatever it is they are intended to measure. Take temperature, for example. All resistors change their value as a function of temperature. This isn’t something we typically favor. Why? Well, apart from the fact that – in the usual course of events – we would prefer the values of our components to remain constant, the higher the current flowing through a resistor, the hotter it gets, which increases its resistance, which causes it to get even hotter. If a resistor gets sufficiently hot, then – much like the Norwegian Blue in Monty Python’s Dead Parrot sketch – it won’t be long before you find yourself the proud possessor of an ex-resistor which has ‘joined the choir invisible’ – see: https://youtu.be/4vuW6tQ0218 This is why it’s important to stay within the resistor’s wattage rating. When the resistor is operating within its power rating, any heat is dissipated into the surrounding environment. If the resistor’s power rating is exceeded, it won’t be able to dissipate the excess heat, and… poof! Let’s take a moment to think about the resistors on our breadboard. These are 1/4W (0.25 watt) devices. The resistors we decided to use with our switches are 10kΩ parts. Since we are running at 5V, from Ohm’s wheel we can use P = V2 / R to determine that the power being dissipated by these resistors is (5 × 5) / 10,000 = 0.0025W, which is 1/100th their rated value. As we discussed earlier in this series (PE, February 2023), in the case of our 7-segment display, the forward voltage drop of the red light-emitting diode (LED) segments is approximately 2V, which means our current-limiting resistors are dropping 5V – 2V = 3V. We don’t need to know the value of these resistors here (we do know them, but we don’t need to) because we selected this value to meet the maximum forward current of the LEDs, which is 20mA. Since we know the voltage (3V) and the current (0.02A), we can use P = V × I from Ohm’s wheel to determine that the power being dissipated by these resistors is 3 x 0.02 = 0.06W, which is approximately a quarter of their rated value. Moving on… a thermistor (a portmanteau of ‘thermal’ and ‘resistor’) is a semiconductor-based type of resistor whose resistance value is strongly dependent on temperature – much more so than in standard resistors. This means that, once we’ve calibrated things, we can use these devices to measure temperature. We aren’t going to play with thermistors here because we have other poisson à frire (fish to fry). If you do decide to experiment with these devices in the future, you need to be aware that they come in two flavors: those with a negative temperature coefficient (NTC), in which the resistance decreases as the temperature increases, and those with a positive temperature coefficient (PTC), in which the resistance increases as the temperature increases. Blinded by the light Another very useful sensor is a photoresistor, which is also commonly known as a light-dependent resistor (LDR). This is a semiconductor-based type of resistor whose resistance value decreases as the ambient light intensity increases (see Fig.2). Practical Electronics | December | 2023 (a) Symbol (b) Device Fig.2. An LDR, aka a light-dependent resistor – these are easy-to-use nonpolarised devices. T ype Dark resistance (MΩ) Light resistance (kΩ) (10 lux) G L5506 0.2 2 to 5 G L5516 0.5 5 to 10 G L5528 1 10 to 20 G L5537 2 to 3 20 to 50 G L5539 5 50 to 100 G L5549 10 100 to 200 Fig.3. Comparison of LDR types. In the dark, an LDR can have a resistance as high as several megaohms (MΩ). As the light intensity increases, the LDR’s resistance can fall to be as low as a few hundred ohms. Since I usually like to play with things, I opted for a 70-piece kit containing ten each of the following LDRs: GL5506, GL5516, GL5528, GL5537, GL5539, GL5549, and MG45. A similar kit is available from Amazon in the UK for around £16 – see: https://bit.ly/3S2430m So, which of these LDRs should we use? I had a ‘fun time’ Googling this question (yes, that was my sarcastic voice). The fun only increased when I found different sources proffered different values for these devices. Also, there appear to be two flavors of the GL5537, and I couldn’t find any information whatsoever on the MG45. To save you the pain, a rough and ready comparison is presented in Fig.3. It takes only a brief glance at this table to realise that these LDR values are all over the place. Two LDRs of the same type may present very different resistance values for the same level of ambient light. What this means is that we typically need to calibrate our code to work with the LDR we are using. Also, their ‘sloppy’ nature means we usually employ LDRs to perform only relatively simple On/Off (Go/No-Go) type functions. There are more precise light-detecting devices available – like photodiodes and phototransistors – but LDRs will serve our purpose here. As discussed in our previous column, if you have a clock with an LED display in your bedroom then – assuming you didn’t buy the cheapest one going – when you turn off the lights, the clock will sense this and reduce the brightness of its display. In this case, it will probably be using an LDR to perform this sensing function. I recently saw a circuit intended to automatically close window blinds when it got dark outside and open them again when the outside light returned. To provide hysteresis (which we might take to mean ‘slowing the response to changes in the stimulus,’ in this example), this circuit employed two LDRs – one to detect when the outside light fell below a certain level and the other to detect when the light rose above a certain level. One of the great things about electronics is that there are always multiple ways of doing the same thing (or, at least, of achieving the desired goal). Contrariwise, one of the things about electronics that can make your head hurt is that there are always… you get the idea. LDR 5V Pot LDR1 VR1 10kΩ A2 A0 Translucent view showing the pins underneath R1 10kΩ GND To the Arduino’s A0 analog pin LDR Trimpot To the Arduino’s A2 analog pin Fig.4. Adding an LDR to our breadboard. 45 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 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 Adding an LDR I don’t know about you, but my breadboard is starting to look a little cluttered, so I’ve decided to remove my two pushbutton switches and their associated pull-up resistors (I can always add them back later) and replace them with one of my LDRs along with a 10kΩ resistor (Fig.4). I’m going to use a GL5516 LDR because… well, why not? LDRs are non-polarised components, which means that – like standard resistors – it doesn’t matter which way round they are connected. You can download an image of the entire breadboard (file CB-Dec23-05.pdf). As we see, we are using the LDR in conjunction with the 10kΩ resistor to form a potential divider, the nitty-gritty details of which we introduced in an earlier column (PE, October 2023). In this case, we’ve connected the 10kΩ resistor on the ground (0V) side and the LDR on the 5V side. Let’s perform a little thought experiment. Suppose our LDR is in the dark, which means it will present its maximum resistance of 0.5MΩ. In this case, the voltage presented to Arduino input A0 can be calculated as the input voltage (5V) multiplied by (R1 / (R1 + LDR1)), which equates to 5V × (10,000 / (10,000 + 500,000)) = ~0.1V. In turn, this will equate to ~21 out of the 0 to 1023 range that can be generated by the Arduino’s 10-bit analogue-to-digital converter (ADC). Now suppose the LDR is exposed to a bright light that causes it to present its minimum resistance. Let’s use the worst-case value of 5kΩ for the GL5516 from Fig.3. In this case, the voltage presented to the Arduino input A0 can be calculated as 5V × (10,000 / (10,000 + 5,000)) = ~3.3V. In turn, this will equate to ~675 when we read it into the Arduino. It’s worth noting that we could exchange the positions of the LDR and the 10kΩ resistor such that the LDR was on the 0V side and the 10kΩ resistor was on the 5V side. In this case, exposing the LDR to dark would cause the values seen by the Arduino to rise, while exposing the LDR to light would cause the values seen by the Arduino to fall. Lighting up our LDR Are you feeling feisty? Me too! Let’s create a little program (Listing 6a) to see how well the real world matches up with our thought experiment (file CB-Dec23-06.txt). Listing 6a. Testing the LDR. All we are doing is looping around reading values from the LDR (Line 21) and writing those values to the Serial Monitor (Line 27). Line 23 is just ‘for show,’ while Lines 24, 25, and 26 ensure the values we display in the Serial Monitor are right aligned (we first used this technique in PE, October 2023). Well, that was interesting I just performed a very unscientific experiment as summarised in Fig.5. I placed my first LDR (LDR 1 in Fig.5) in full darkness (I put a tiny cardboard box over it). In this case, the values I saw hovered around 5, which was a little lower than we predicted. My ‘Low Ambient’ test involved removing the box to expose the LDR and turning the room lights off. It’s a dark gray late afternoon outside as I pen these words, and most of the light in the room is coming from my computer monitor, so I was a tad surprised to see readings hovering around 569. Next, I turned the room light on in the center of the ceiling. This isn’t all that bright because it boasts only four Edison Bulbs. ‘Still and all,’ as they say, the readings jumped up to hover around 790. Turning on a small bendy-neck table lamp directly over the LDR caused the readings to jump up to around 997. Finally, I whipped out the trusty tactical LED flashlight (torch) I always carry about my person in case a Zombie apocalypse breaks out. This is super bright, but it caused only a small rise in the values coming from the LDR. All this tells us several things, including the fact that the LDR’s ‘Dark Resistance’ is significantly higher than 0.5MΩ, while its ‘Light Resistance’ is significantly lower than 5kΩ (I can’t be bothTest LDR 1 LDR 2 ered to do the maths). It’s also Full dark 5 27 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 Low ambient 569 565 Room light 790 786 Table light 997 981 Zombie light 1004 989 Fig.5. Testing two LDRs. Practical Electronics | December | 2023 Online resources 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. clear that the LDR saturates around the table light level, after which increasing the amount of light has little effect. Out of interest, I swapped my existing LDR for another of the same type (shown as LDR 2 in Fig.5). To be honest, the most surprising thing to me here was that the results from the two devices matched each other so closely. If a were a better man, I would test all 10 of my GL5516 LDRs, and then perform the same tests on the other LDR types. Sad to relate, I’m not a better man. Also, my wife (Gina the Gorgeous) is champing at the bit because she feels I should be spending ‘quality time’ with her as opposed to experimenting with an LDR (‘significant others,’ what can you say?). Next time In my next column, among myriad other things, we are going to use the values read from our LDR to control the brightness of our 7-segment display. Do you have any thoughts on how we will achieve this? I will leave you pondering this poser. Until next time, 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 Teach-In 8 CD-ROM Exploring the Arduino EE FR -ROM CD ELECTRONICS TEACH-IN 8 FREE CD-ROM SOFTWARE FOR THE TEACH-IN 8 SERIES FROM THE PUBLISHERS OF This CD-ROM version of the exciting and popular Teach-In 8 series has been designed for electronics enthusiasts who want to get to grips with the inexpensive, immensely popular Arduino microcontroller, as well as coding enthusiasts who want to explore hardware and interfacing. Teach-In 8 provides a one-stop source of ideas and practical information. The Arduino offers a remarkably effective platform for developing a huge variety of projects; from operating a set of Christmas tree lights to remotely controlling a robotic vehicle wirelessly or via the Internet. Teach-In 8 is based around a series of practical projects with plenty of information for customisation. The projects can be combined together in many different ways in order to build more complex systems that can be used to solve a wide variety of home automation and environmental monitoring problems. The series includes topics such as RF technology, wireless networking and remote web access. PLUS: PICs and the PICkit 3 – A beginners guide The CD-ROM also includes a bonus – an extra 12-part series based around the popular PIC microcontroller, explaining how to build PIC-based systems. £8.99 INTRODUCING THE ARDUINO • Hardware – learn about components and circuits • Programming – powerful integrated development system • Microcontrollers – understand control operations • Communications – connect to PCs and other Arduinos PLUS... PIC n’MIX PICs and the PICkit 3 - A beginners guide. The why and how to build PIC-based projects Teach In 8 Cover.indd 1 04/04/2017 12:24 PRICE £8.99 Includes P&P to UK if ordered direct from us SOFTWARE The CD-ROM contains the software for both the Teach-In 8 and PICkit 3 series. ORDER YOUR COPY TODAY! JUST CALL 01202 880299 OR VISIT www.epemag.com Practical Electronics | December | 2023 47