Hi. One of the coolest things about microcontrollers is that you can use code running on your chip
to interact with the real, physical world. In previous videos, we have shown you how
you can sense the real world with things like photodiodes, temperatures sensors, or even
mechanical switches. We have also talked a little bit about how you can actuate the real
world with things like motors and servos. But we have yet to talk about one of the simplest
forms of electromechanical actuators out there: the solenoid.
In order to demonstrate the solenoid concept, and bring a little Christmas cheer to the
office, we decided to put together a xylophone that plays itself.
In order to build our robotic xylophone, the first thing we had to do was, well, build
a xylophone. We found these scrap aluminum bars in our workshop, and when we gave them
a hit, they made a pretty nice tone.
So we decided to see if we could tune these bars to the specific musical notes we wanted.
We found a book called "The Theory of Sound," published in 1877, and written by the now-famous
physicist Lord Rayleigh.
There's an entire chapter called "Lateral Vibrations of Bars," which sets up the fourth-order
differential equations that are involved in even a simple model of this problem.
Basically, when I hit the bar like this,
we've got what's called a free-free bar, which means it's not being clamped at either end.
The initially flat bar will bend a little bit like this, and then a fraction of a second
later, it will spring back the other way. And just like a mass and a spring, it will
keep flexing back and forth, and pushing on the air around it, which makes the sound waves
that we ultimately hear.
So let's figure out what factors influence the frequency that our bar will vibrate at.
First, there's the material itself. A more dense bar will vibrate at a lower frequency,
because there's more mass to accelerate as the bar springs back. Then there's the stiffness
or elasticity of the material. A less stiff material will vibrate at a lower frequency
because it has to displace farther before it springs back. Then, there's the geometry
of the bar. A thinner bar will vibrate at a lower frequency because it's less stiff.
And finally, there's the length of the bar. A longer bar will vibrate at a lower frequency
because the wave has to travel longer. So, for our bars, we're going to keep these first
three the same, and we're just going to vary the length to vary the frequency of our bars.
If we hit the books again, we'll find that, "for a given material the frequency of vibration
varies inversely as the square of the length." That means that if we take one bar and measure
its length and frequency, we can figure out how long a bar needs to be to get a different
frequency.
That's exactly what we did, making a spreadsheet of different notes that we wanted to make,
and then cutting and milling bars down to length. Each time, we made the bars a little
bit longer than our equations said to, and then measured the frequency again and figured
out how much to cut off to get as close as possible to the final frequency.
The last part of making the bars is figuring out how to hold them. If we constrain the
bars too much, they won't be able to vibrate. But if we jump back to the books, we'll find
this graph which shows the shape of vibrations in the bar. If we hold the bar gently at these
two points, called nodes, we'll interfere as little as possible with the vibration that
we're looking for. So we drilled holes at these positions, about 22% of the way from
either end of the bar, and mounted the bars on rubber washers.
At that point, we had a plain old xylophone.
So the next step was to build electromechanical actuators to let the microcontroller strike
each bar at the right time.
Solenoids are one of the most basic electromechanical actuators. To understand them, we have to
back to physics, and remember that a current in a wire will create a magnetic field around
it. Then, we can make a loop of wire, and now, the magnetic field from all of these
little segments of wire add up, so we get a field that's strongest in the middle, and
weaker on the outside.
Finally, we can take one long piece of wire and wrap it around as a solenoid coil. Now
we have a bunch of loops, and the magnetic field contributions from all of these loops
add up. This lets us get one field that's really strong in the center.
At this point, we've made an electromagnet, where we can turn a magnetic field on and
off with just a current. So just like a permanent magnet is attracted to iron and steel objects,
so is our electromagnet when there's a current running through it.
Now, we're going to take our coil, and add a steel slug. When the coil is off, gravity
is going to pull the slug down. But, when we put a current through the coil, it'll start
acting like an electromagnet, and the steel slug will be attracted to the strongest part
of the magnetic field. This is near the center of the coil, so the slug will jump up a little
bit.
We can demo that here, and you can see the steel slug moving up and down as I apply and
remove current.
We made our own solenoid forms by turning down some plastic on the lathe. First, we
cut channels on the outside to hold our wires. And then, we drilled out the center to act
as a channel for our solenoid's moving slug. We tapped a small hole on the other side so
we could mount the coils on our xylophone. Finally, we wound several hundred turns of
magnet wire around each coil, fixed the whole coil with a little bit of superglue, and carefully
stripped the enamel insulation from each end with a knife.
Right now, we only have an actuator that can pull into the center of the solenoid, but
we need something that will shoot up toward the xylophone bar. The most basic mechanical
option would be to put a little non-magnetic rod on top of the slug, but we wanted to use
less electrical power and get more force, so we could have a louder xylophone strike.
So we ended up modifying the solenoid concept a little bit.
We attached some permanent magnets to the top of our steel slug, so now there's always
a magnetic field around it. When we only have one magnet and one piece of iron, we can only
have an attractive force. But once we have two magnets, we can have either attraction
or repulsion. One of these magnets is our permanent magnet, guided by our steel slug,
and the other one is the electromagnet. So we can draw this in the resting position,
with the slug and magnet just above the center of the coil. And then when we put a current
through the coil in the right direction, we'll actually get a force that pushes the down
on the coils and pushes up on the magnet and steel slug. Now, the math for magnetic fields
is generally a bit tricky, but let's just say that we get a lot more force for a lot
less current by taking advantage of permanent magnets.
By letting current run through the solenoid briefly, the slug will jump up and hit the
xylophone bar.
To let the microcontroller actuate each coil, we've got to build a circuit that can handle
this switching action.
We're going to use a MOSFET as a switch, which we've talked about in many previous NerdKits
videos, such as the Servo Squirter video, Motors and Microcontrollers 101, and the iPhone
remote-controlled car video. Now in this case, we're going to have our microcontroller controlling
the gate of the MOSFET, which acts like a switch, opening and closing and letting current
flow through our solenoid coil.
Now there's one special trick here, because this is definitely an inductive load. That
means there's energy stored in the magnetic field, so when this coil goes from on to off,
all of that energy needs to go somewhere. If we don't do anything about it, then the
voltage at this node is going to rise very high very quickly, until eventually this MOSFET
breaks down and lets some current flow, even though it's supposed to be off. To fix that,
we're going to do what we usually do, which is to put a reverse-biased diode across the
inductor. In this case, we decided to do something special, which is to use an LED. So, when
this switches from on to off, the voltage starts to rise, but then current is allowed
to flow through the LED. We take some of the energy from the magnetic field and turn it
into light energy. It's basically free energy that would have been wasted anyway, and now
we get to actually see when each of our coils is done firing. So, anyway, this is a little
basics of controlling inductive loads. For more information, take a look at our Motors
and Microcontrollers 101 video.
Finally, let's put together the bars and the solenoids. We just send a current pulse through
the solenoid on for a few milliseconds, and we've got music.
The code running on the microcontroller is actually not all that complicated. There are
two main timing issues you need to worry about: timing when the next note should played, basically
the tempo of the song, and timing how long you need to pulse each coil on for when playing
each note. The amount of energy delivered to the solenoid depends on how long you pulse
the coil on for, so leaving it on for longer gives you a louder ping.
We simply use two separate timers, and timer interrupts to handle these two time intervals.
There is a little bit of math done to change the beats per minute of the song into the
value needed to set the output compare register.
At this point, we could have just driven each individual coil directly from our MCU. This
would have taken 8 digital output pins on our microcontroller, and it would have worked
just fine. However, we thought this would be a great opportunity to introduce a very
useful digital component: the shift register. For this project, we are using an 8-bit shift
register, which means it has 8 digital outputs that we can control. This is a digital component,
so you can interface with it directly from your MCU.
A shift register will typically use two inputs: a data input, and a clock. When the clock
line pulses, the shift register takes the state of its output pins and shifts them over
by one, shifting in the current value of your data input. The state will be held until you
pulse the clock line again, and the bits shift over again, and the new bit gets shifted in.
Say we wanted to have the 2nd note go on, and all the other notes off. If we were using
the straight MCU pins we would just turn that bit high, but with a shift register, we need
to shift the bits in one at a time. First, we need to shift in 6 zeros, because remember
we want all the other notes to be off. So we set the data line low, and pulse the clock
to shift in the first zero. Then again to shift in the next zero, and so on until we
have 6 zeros. We then put a 1 on the data input, then pulse the clock to shift it in.
Then we shift in the last zero. To clear the shift register, we would just shift in 8 zeros.
Note that you can shift in any combination of 1s and 0s you want. They are just like
pins on an MCU you can set.
This may sound like a lot of work to go through when I could just set the MCU pins, but notice
that I am able to set 8 outputs with only 2 inputs. Even more interesting is that I
could chain another 8 bit shift register together by connecting the data input of this register
to the last output of the previous register, and connecting it to the same clock line.
Now the bits would continue propagating through, and I could set 16 outputs with the same two
inputs. With a shift register, you're basically trading time for more pins.
To write the code to do this, you could write a function that sent out a full byte to the
shift register, toggling a digital clock pin at the right time. However, there is a module
on board the chip, the SPI bus, that happens to do exactly that for you. The SPI bus takes
a byte that you give it in code and it shifts it out bit by bit over its MOSI pin, toggling
the clock line at the right time. The SPI bus has the advantage that it can clock out
the bits much faster than you would be able to in the code.
So in our code, we use the SPI bus to set the shift register, which activates the right
coil, which induces a magnetic field, which repels the magnet, which moves up to strike
the bar. After a little while, we shift in 0s, and the slug moves back down. Then repeat
when it's time for the next note. Do this in the right order, and you get Jingle Bells.
We hope you learned something about solenoids, and enjoyed watching our robotic xylophone.
For more information about our kits, or more videos like this one, please visit us at www.NerdKits.com.