Let Randomness Ring
Wind chimes make a pleasant sound during the warm months when windows are open. But wouldn’t it be nice to simulate those sounds during the winter months, when your windows are shut? In Part 1 of this project article, Jeff builds a device that simulates a breeze randomly playing suspended wind chimes. Limited to the standard 5-note pentatonic chimes, this device is based on a Microchip PIC18 low-power microcontroller.
During the gloom of our winter months in the Northeast, I’m constantly looking for reminders of warmer, sunnier greener days. To that end, I pay attention to where the sun rises each morning. Since this always takes place in a view east, I get to enjoy the first rays of the day as they pop over the ridge line across Crystal Lake. With a cup of joe to warm heart and hand, I can feel the gloom melting away as Sol begins its rise a bit more north each morning. Even though winter doesn’t officially start until the winter solstice, that’s the point at which I feel an awakening. While there are outside activities that flourish in the colder seasons, I enjoy the “outdoors” more when not encumbered by all the winter gear.
When the fruits of summer die away, I have to sigh at not being able to grab a tomato off the vine and just eat it, as I would an apple. There are a number of plants that we put outdoors for the summer. If I remember to bring them inside before the frost, I can be accompanied by a bit of green for the winter. Spending time watering and pruning these friends helps to keep alive the memories of summer. There are sounds of summer I’ve wished I could savor during the winter as well. The summer breezes enhanced the soundscape thanks to the tinkling sounds of wind chimes. Our neighborhood has chimes of all sizes. When the wind blows, the neighborhood becomes a concert hall.
Sadly, the wind doesn’t blow indoors—unless you have a rather drafty house. To that end this month’s project will entertain artificially engaging these chimes, sans breeze. My initial thoughts centered on creating a mechanical striker that could be added to an existing chime. The more I thought of it, the less I liked being limited to five chimes or notes. If I were going to build a circuit for striking chimes, why limit it to five? Is there some magic to that number of tubes?
Chimes are bells without an internal clapper. In Southeast Asia, the remains of wind chimes made from bone, wood, bamboo and shells have been dated back to more than 1,000 years BC. Bells were believed to ward off malevolent spirits. Today most commercial chimes are made from aluminum, which has the lowest internal damping, to achieve the longest and loudest sounding chime. Various length tubes are hung in a circle, which allows a single central clapper to swing and contact any of the suspended tubes as shown in Figure 1. Chimes are not limited to this arrangement. I’ve seen a unique combination mobile/chime made from suspended silverware. No clapper was necessary, because as the utensils spin, they collide with their neighbors, giving off their own unique rings.
Orchestral chimes (Figure 2) are arranged chromatically, like the black and white keys of a piano. They are usually struck with a mallet. The ability to play a particular tune may require any of these notes. The scale we learn as children—Do-Re-Mi-Fa-So-La-Ti-Do—has 7 notes per octave, where an octave is a doubling (or halving) of a note’s frequency. In the presented scale, Do and Do are an octave apart, with the second Do twice the frequency of the first Do. While we may sing Do-Re-Me, the actual notation of a scale is A-B-C-D-E-F-G-A. Again, these are the white keys on a piano. But what about the black keys? The chromatic scale includes both white and black keys. There are actually 13 notes (12 intervals) per octave in a chromatic scale. Moving up the chromatic scale has each note one semitone higher than the last. A semitone is the change in frequency equal to a ratio of 1.05946—the 12th root of 2—also called a half step/tone. It is the smallest musical interval commonly used in Western music.
When we sing the scale (Do-Re-Mi) and start on C we are singing in the key of C and all the notes fall on white keys. Should we start on D, we need to use some of the black keys to keep the same (Do-Re-Mi) spacing. You’ll notice that when two adjacent keys are played together, the sound is unpleasant. If we play any two notes in the key of C (the white keys) together, we find that only those a semitone apart sound unpleasant—that would be B-C and E-F. These are adjacent white keys. If we eliminate, for example, B and F, then we get a 5-note octave (A-C-D-E-G-A), in which all notes sound pleasant in any combination. This is called the pentatonic scale. So why this explanation? As it turns out, the notes in a pentatonic scale are those most widely used in wind chimes!
LENGTH VS. FREQUENCY
In general, the length of a chime sounds at a frequency equal to a constant / length2. The constant is a combination of terms that essentially describes the material being used. So, if we took a piece of 1/2″ copper tubing and cut it to 14″ we could measure the frequency as about 440 Hz (A, 4th octave or “A4”). If we plugged these into the equation, we can do the math to get the constant as follows:
440 Hz = constant / 14″ × 14″
constant = 440 Hz × 14″ × 14″
constant = 86,240
If we wanted to find the tubing length (L) for E4 (E, 4th octave) (329.6 Hz), we could use this length and the constant as follows:
L = 16.18″
The point is, once you determine the constant for the material chosen, you can calculate the length of the tube for any note you want—if you use the same material for all tubes. One way to measure the frequency is with a microphone and oscilloscope or some tuning program for your PC or cell phone. Remember that your chime will most likely contain overtones or harmonics—multiples of the fundamental frequency.
Now that you have a little background on chimes, selecting notes and cutting material into proper lengths, let’s get into some circuitry. I could easily mount a small fan next to a standard chime and be done. But what fun would that be? I want to have control over the chimes, and so I’ll be using one solenoid per chime tube. For a pentatonic chime, only five solenoids will be needed. While this project can be limited to five, I have enough I/Os for 16 chimes. I can use the chromatic scale to cover more than an octave’s worth of notes.
I have some 12 V solenoids (JF-0530B) that require about 300 mA to stroke. The stroke is about 1/2″ and includes a spring return. To drive the solenoid, I am using an old workhorse: the ULN2803A from STMicroelectronics. This 18-pin device contains eight Darlington transistor drivers with internal diodes from each output to VDD (across the solenoid inductor). A channel can sink up to 500 mA from up to 50 V. That’s a maximum single channel power of 1 W with a package maximum of 2.2 W.
The circuit contains two 2803As sequenced by a Microchip PIC18F26K22 microcontroller (MCU) (Figure 3). Excess I/O is used for a serial communication channel and some push buttons and LEDs. You’ll note a couple of modules used with this circuitry. A DC-DC converter is used to go from battery voltage to 12 V for the solenoids. I used the XL6009E adjustable DC-DC switching boost converter from Addicore. This project requires the use of a Li-ion battery, because the initial current draw for a solenoid will be almost 1 A at the lower battery voltage. To charge the battery, I added a Li-ion charger circuit that can be plugged into a USB port—the USB LiIon/LiPoly charger – v1.2 from Adafruit.
The first thing to determine is how long a pulse must be applied to the solenoid to achieve a full thrust. I was going to need a time base anyway for note intervals, so I set up a timer interrupt for 1 ms, using timer0. At each interrupt, the register Note0Count is tested for zero. If it’s not zero, it’s merely decremented. When it has decremented to zero, it clears output bit P0. Now, from the main loop I can load Note0Count with a value, and then set the output bit P0 whenever I see a button push. The interrupt will handle turning off the output bit after Note0Count has decremented to zero. I determined Note0Count=100 (ms) was a good pulse width to use on this solenoid. This simple test can be repeated for each of the 16 outputs, as in Listing 1, so any output that is turned ON will be turned OFF after 100 interrupts.
Now let’s figure out when to turn on an output bit. The MCU has some EEPROM that can be used to store changeable information. I’m going to use this area to store values that relate to the notes I will be using for the 16 chimes. Sixteen values are stored into the first 16 EEPROM positions, and these correspond to outputs P0 through P15. If you were going to use only five pipes of the pentatonic scale, then you would save their values in the first five locations and leave the rest empty. Actually, for a basic 5-note pentatonic wind chime you wouldn’t need to do this. Just randomly enable these five outputs, and you’re done. However, I want to identify the notes I plan to have connected to the 16 outputs, so I have saved the values 51-68, which relate to the 16 notes of the chromatic scale beginning with D♯ 3/E♭ 3 (D sharp or E flat of the 3rd octave) and ending with G♯ A ♭4 (G sharp or A flat of the 4th octave.)
By saving these values in EEPROM, I have a list of the chime notes connected. When I get a request for a particular note, I can look through the list. If there is a match, I know which output to pulse. Now, this next test might seem like a roundabout approach, but it will test the look-up routines. I want to test all the notes, so let’s play the scale. Since I know the tubes are arranged lowest to highest, I could just pulse each output, P0 through P15, to play the scale. Instead, I’m going to grab a request from the EEPROM locations 0-15 (which will be a stored note), then use the find routine to see if it is in the list (which it will be), then play the note that was found. The find routine tests all the EEPROM values for a match and returns a flag with the results.
To emulate the fluctuating movements of a breeze pushing the clapper among a chime’s tubes, we will need some kind of randomness. Creating a random number generator without a hardware source is no simple task. Because all operations run off the same clock, it is difficult to get any kind of randomness. Entire articles could be dedicated to this, so I’m not going to do anything more than run a timer and take bytes from its LSByte. I’ll rely on the fact that different values will take different paths of execution to keep things interesting.
When I want a random number, I’ll call the NextRandom routine that merely reads the present LSByte from timer 1. If the MSBit of that value is set, I’ll consider that value a Note. If it is clear, I’ll consider that value a Duration. If the value is treated as a Note, then use FindNote to see if it is a playable note (one that’s in the EEPROM). If a match is found, then the note can be played. To prevent multiple drivers from being enabled at the same time, all the output bits are inclusively OR’s together. The code waits for this to be zero. Should the value not match a note in the EEPROM, then it’s thrown out and the code jumps back for another random number.
If the test on the random value determines it should be treated as a duration, then we need to determine how long the duration will be. You might want to simply go into a loop decrementing this value until it reaches zero as a form of delay. But I have other plans, so stick with me here as I delve into a bit more music theory.
Most music has some indication of the tempo, speed or pace of a song. This description might be in words or numbers, but it all boils down to BPM (beats per minute). You can think of it as the rate at which you might tap your foot while listening to it. Note the word “Andantino” in the music for piano in Figure 4. This means a relaxed moderate tempo of around 64-72 BPM. The five horizontal lines create the “staff” on which the notes are placed. The notes can be placed on a line or a space, with each representing a specific frequency. On the top staff is the “G” clef (looks like an ampersand), where its curl encircles the second line from the bottom. Remember the scale we talked about earlier? If the second line represents “G”, then starting with the space below the first line, the spaces and lines on the staff represent: …D-E-F-G-A-B-C-D-E-F-G… where the bold letters are lines, the non-bold letters are spaces. This particular piece of music has two staffs—the lower (with an “F” or bass clef) for the left hand, and the upper (with a “G” or treble clef) for the right hand. An F-clef is used to designate notes that are much lower than those on the G-clef staff.
The fraction (time signature) on the staff denotes type and number of beats. A beat is identified by the denominator of the fraction, in this case 4, or a quarter note. The numerator 3 indicates that there are 3 beats (quarter notes) to a “bar.” Bar lines are vertical lines on the staff. They divide the music into bars, which are segments of music containing a specific number of beats. In the lower staff, you can see 3 quarter notes (solid dot, with a stem either up or down) in a bar. Any dots on the same stem are notes that are played at the same time—remember, a pianist has five fingers on each hand. Every bar in this piece of music will have 3 beats. However, other duration notes can be used in any combination as long as they add up to 3 beats. Notice the upper staff has four notes, but their symbols represent different durations. We need to know more about durations.
Since we began with the quarter note, let’s start there. The quarter note has the duration of one beat. An eighth note has half the duration of a quarter note (two notes per beat). A half note has double the duration of a quarter note (two beats per note). See Figure 5 for the meanings of the different note symbols. The idea here is that for the most part, each note is a factor of 2 shorter or longer in duration than its neighbor. This idea makes it easy for all musicians playing the same piece of music to keep their notes in sync with one another, because the conductor waves the baton at the appropriate BPM rate of the music. They just need to count along with each bar, so they don’t lose their place.
We’re going to go back to Listing 1—our interrupt routine—to handle this, because it’s already being executed every 1 ms. Based on a BPM of 120, we can figure a beat is equal to 500 ms.
So that’s 0.5 s = 500 ms
We will need to be able to get down to 125 ms for the fastest note (sixteenth), and that will become a tick. I use the register DurationCount set to reload to 125, to count the milliseconds (interrupts). The remaining code will be executed only once every 125 interrupts or 1 tick. This tick of 125 ms was based on the BPM of 120. We can easily set BPM to some other value—say 50-200—and set the register DurationCount to create the proper timing for all notes based on that BPM. I’ve defined seven constants based on the tick. A single tick equals the Sixteenth note, The Eighth note is double that or 2 ticks. The Quarter note is double that or 4 ticks, and so on up to 32 ticks for the Double (2 × Whole) note.
If the DurationCount in the tick routine is zero, then there is nothing to do, so we can simply exit the interrupt. If it has been set to some value, then it is simply decremented each 125 ms tick. When it is decremented from 1 to zero, the DurationDone flag is set. This interrupt is doing most of the work for this whole project so far. It not only times individual pulses—turning off when an output pulse times out—but it also keeps track of different note durations and flags when the duration times out.
MORE ON DURATION
Unlike most instruments, the chime essentially has no duration associated with it. That is, once struck, it will continue to vibrate until it has lost all of its energy. When changing notes and striking a new chime, the last one may continue to sound. That’s unlike other instruments, where each note lasts only for the duration as written on the staff. Still, we must pause for that note’s duration, even though its vibrations will be independent of its notated duration. So, we actually have two durations—one between notes, and possibly one of a pause while no note is played.
Referring back to Figure 4 and Figure 5, in addition to notes, the music staffs might also contain “rests”. A rest is a duration with no note struck. All the rests are designated by different symbols corresponding to note values (Figure 5), so the musician knows how long to pause before playing the next note and can keep in sync with, in this case, 3 beats to a bar.
Every note struck must have a duration associated with it, unless the notes are to be played simultaneously—like the six notes played when a guitar is strummed. As in all music, once a note has been played for its duration, the following operation might be another note or a rest. And this brings us back to talking duration in relation to our random number generator.
We left off trying to figure out what duration to use based on the random number generated. Unlike other music, wind chimes are truly random in that notes and rests are not limited to those times notated in a musical arrangement. For this part of the project, the random number will simply be placed into the DurationCount and the interrupt will count down that number of ticks (125 ms/tick). Durations will therefore last between 1 and 127 ticks (0.125-16 s). Listing 2 shows the debug output for what we can expect this part of the project to create. You probably have a mental image of how the mechanics of this project will be constructed. Next month I’ll go into that, and will conclude with some additional code. You may have an idea of where this project is headed if you followed the bread crumbs to which I’ve alluded.
The project has been prototyped using point-to-point wiring of the discrete components and modules described in the schematic (Figure 3). You can see the modules mounted atop the protoboard in Figure 6. The 16 solenoid outputs will drive the strikers, one for each of the chime’s tubes. Stay tuned for the finale.
Additional materials from the author are available at:
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JUNE 2019 #347 – Get a PDF of the Issue