Notes for today's Lab:

Assuming you get caught up on the first two labs, you'll be assembling a robot. That part is pretty easy. You should already have the robot (black box) and the motor driver chip (small board with two strips of holes on its edges).

That motor driver is going to be very handy for your labs, because it provides an H-bridge (which we remember from lecture, right?). However, because this part of control is done for you, we kinda glossed over switching technologies for motors.

Why do we need to talk about switching?

The GPIO (General Purpose IO) pins of your microcontroller board should normally put out up to 20mA (40 at the absolute maximum). Anything more than that, and expect something bad to happen. Motors draw far more current than that, so you can't power then directly from the board.

So, what's the solution? Use power from somewhere else! But that means we still need to find a way to include the controller. Basically, we want to use the controller for the logic side.

How can we switch?

This is a super-complicated question, but a simplified answer is: either via solid state switching, or electromechanical relay. But would both be appropriate for this?

Solid state

When we talk about solid state switching, we're talking about things like transistors. Generally, we're talking about a field effect that (interjection: remember, we aren't in a Physics course!) lets you control the flow or halting of current across a semiconductor through the application of a charge to another pin. In other words, electricity goes in one pin and out another, or it doesn't, based on the status of a third pin.

There are lots of factors, both pro and con, but the two biggest factors in favour of solid state switching are that it's very fast, and (in the absence of moving parts) they tend to last very long. However, you may have restrictive limitations on current/voltage, loss of voltage, problems with electrical isolation, etc.

Electromechanical relays

Ever had a space heater with a thermostat? When it thinks it's cold enough to turn on, you hear a click? That click is coming from the relay.

Picture two metal fins, very close to each other, but not touching. This acts like a normally open switch. Two pins to the relay control a small electromagnet, which pulls the fins together, closing the circuit. Since this creates a solid electrical connection, you can now have full current/voltage across it (subject to the same sorts of ratings limitations that all electric components have).

Point is: it's reliable, and it's also electrically isolated from the electromagnet. e.g. the electromagnet might work at 5V DC, but the relay itself could easily (and safely) control 110V AC.

So, what's the problem, right? Well, first off, since these do have moving parts, they wear out faster. They might last for ten thousand, even a hundred thousand cycles, but solid state will last longer.

But there's a far bigger concern: remember differential drive? i.e. when you turn one wheel faster than the other wheel, so you can get a smooth turn? Even speed control in general? We use PWM for that, right? (If you'll recall, PWM is when you turn something on and off really quickly; the portion of that time spent in the on state is its duty cycle)

The actual problem

It takes time to switch; to switch anything. But not all switching times are the same (or even of the same order of magnitude).

Solid state switching simply needs to create an electric field. That's very fast. But electromechanical relays need to energize a coil, and then wait for a metal fin to sway to the side to make contact. It's a small distance, but it's still a macroscopic one (maybe one or two millimetres).

Is that a problem? Well, by default, the PWM of your microcontroller is normally 490Hz (two of the pins might be 980Hz). That may not sound very fast, but it's far too fast for a normal relay to close.

Then, what happens? Well, for the on portion of the duty cycle, it tries to close. For the off portion, it tries to open. Since neither phase lasts long enough to actually complete the stroke, that metal fin ends up just wiggling in place, very quickly.

Demonstration

At the supply station, there should be an Arduino connected to the computer above it (hopefully turned on, so it can receive power). Connected (loosely) to the Arduino is a simple board with two forms of switching on it.

The board receives both power and a signal from the Arduino. That signal uses various pulses to attempt to turn something on. The board has a couple of LEDs to try to light up, and a small black switch to choose which switching technology to use: an electromechanical relay, or a MOSFET (note: I know it looks very similar to the 7805 voltage regulator. Electronic components typically come in standardized shapes — known as packages. The 7805 and that MOSFET both just happen to come in the TO-220 package).

Try the MOSFET first. Press the circular reset button on the Arduino, and you'll see the LED on the board blinking. Now, try sliding the black switch to choose the relay. Its LED can't light up, right? That's because its desired state is changing too quickly.

In fact, if you listen very carefully, you might even be able to hear the efforts of the metal fin attempting to move back and forth. Actually, you should get very close, and listen very carefully. Some frequencies seem to trigger a natural resonance with the relay (and are thus louder), but there are quieter ones as well...