IAN LANG ELECTRONICS

This is the last circuit in the Oomlout book and if you've done the previous ten you should have a pretty good grasp of the basics of the LED, some knowledge of the NPN transistor and what a snubber or flyback diode does now. As the Arduino was designed to bring microcontrollers out of the R&D labs and into the hands of the public, up to this point I've worked to the principle that you might not be an electronic engineer or technician, but an artist ,designer, mechanical engineer. hobbyist etc looking for an introduction to microcontrollers. For the rest of this lesson I'm going to work to the same principle, but for those of you seeking a deeper understanding of the theory, all the components used in this circuit are discussed in depth in other parts of this website as is Ohm's Law and electron theory. If you click on the links below and to the left, it will open up these pages in new windows. You don't have to read them in order, but if you are new to electronics or electrical principles I recommend that you do as they follow a structured learning path.

# CIRC-11

Go Back The Resistor The BJT The Diode The Relay The LED Ohm's Law Electron Theory

And so on to the circuit then. It's a fiddly construct, I'd start by shoving the relay and diode in fist and connecting them together. Then shove in the transistor and it's base resistor, connect emitter to ground and collector to the load (the relay) and that's the important stuff done. Here's the circuit as it appears in the book:

Copy & Paste Code from Here:

Assuming it's all gone well, what should now happen is that one LED comes on when the other goes off.

Messing about a bit with this, take the jump wire currently in place in row 16 (if you've followed the map, if you haven't it's the one that's furthest away from the coil end of the relay) and place it on the same row but on the other side of the relay. It should still work. That's because there are two switches in this relay- double throw.

Oomlout suggest you can watch the back-emf pulse: great idea so let's do it. Stick an LED in there instead of the diode but make sure YOUR CIRCUIT IS UNPOWERED before you do it.

Right then let's look at the code:

int ledPin =  2;    // Relay connected to digital pin 2   <-----Change this to pin 2

// The setup() method runs once, when the sketch starts

void setup()   {

// initialize the digital pin as an output:

pinMode(ledPin, OUTPUT);

}

// the loop() method runs over and over again,

// as long as the Arduino has power

void loop()

{

digitalWrite(ledPin, HIGH);   // set the LED on

delay(1000);                  // wait for a second

digitalWrite(ledPin, LOW);    // set the LED off

delay(1000);                  // wait for a second

}

Easy-peasy lemon squeezy this one. All that happens is we set pin 2 high or low providing an input signal (or not) to the base of the transistor, which then allows the relay coil to energise (or not) and become magnetic (or not). If it's magnetic, it moves the contact one way, if not, the contact being on a spring goes back the other. By sticking a delay in there we can control the time in either state in terms of milliseconds.

Why do we need that transistor and resistor? Well, the relay needs a current before it can operate. The minimum current it needs before the magnetism becomes sufficient to activate the contact switch into moving is known as the pull-in current. Your Arduino will struggle to provide it, and it might even break.

The transistor on the other hand doesn't need much current at all before it begins to conduct, and when it does it can easily conduct the pull-in current. Your Arduino board will supply it without breaking sweat.  The only way the relay has back to ground is through this transistor. If the pin's high, the base of the transistor has an input, the transistor conducts, and the relay coil has a path to ground and becomes magnetised. If not, the transistor does not conduct, the relay is isolated and the coil can't magnetise.

The transistor's base current needs to be small, otherwise it will gobble up as much as it can get, overheat and quickly destroy itself.

Right, then, blinky LEDs are quite nice to look at but we certainly don't need a relay to operate them. We could just do it straight from the Arduino board. What we do need a relay for is great big currents that would break the transistor. Although the motor supplied can be run through the transistor without harming it, a bigger one would set it on fire. So we run it through the relay which can deal with really meaty currents. The one that came in my kit says it can handle 1 Amp at 30 Volts, which is thirty Watts, and that would blast the little transistor into the stratosphere. We'll use the motor supplied in your ARDX kit to illustrate the example.

Get rid of your LEDs and their 560 ohm resistor  then, and stick the motor in between where one of them used to be.  When the relay clicks, you'll see the motor comes on and when it clicks again the motor goes off. The circuit is working exactly the same way.

Now take this part:

digitalWrite(ledPin, HIGH);   // set the LED on

delay(1000);                  // wait for a second

and change it to this:

digitalWrite(ledPin, HIGH);   // set the LED on

delay(5000);                  // wait for a second

Depending on where you stuck the lead of the motor it will either spend more time on than off or more time off than on. Stick it where the other LED used to be and it'll do the obverse. This is because we've told the digital pin to stay high for a longer time than it stays low. Therefore, the relay coil is magnetised for longer than it is not, and the contact stays longer on one side than it does on the other. This way we could turn a conveyor belt for a set amount of time if we wished.

So far we've had the motor going only one way. What if we wanted to get it so the motor turns one way if we want it to, and the other way if we require?  We need to make something called a H-Bridge. We could do this with power transistors but it's far easier with a DPDT relay. Oomlout have kindly provided a schematic based on this circuit so let's do it.

You may find your USB port can't handle the power needed for this and if that's the case try a fresh 9V battery.

You could put 120 ohms of resistance at one terminal of the motor too. This has the advantage of slowing your motor down to the point where you can see what's happening but may mean that your motor can't overcome it's inertia.

Put a pointer on it, (if you haven't got one rig one out of some old bit of plastic) and if the motor can't overcome it's own inertia just give it a flick with your finger and it should start spinning. Alternatively you could route the actual power supply independently of the Arduino Board.

And that's all there really is to a relay, it's just a kind of automatic switch.  The control terminals (here controlled by the Arduino via a transistor) use a very small current generated by an electronic circuit to control a much larger one generated by a higher-power electrical. The very first digital computer was worked by relays, until Post Office engineer Tommy Flowers realised you could use valves to do the job quicker and quieter. (Go to Bletchley Park in Bucks to see this. I spent all day there and could have done with a week).

And that concludes not just this lesson but also the whole of the book supplied by Oomlout. I hope that those of you who were a little foggy on the  "dark art" of microcontrollers are now more well informed, and that those of you who knew something now know something more. Over the coming few weeks I intend to make and post more circuits using only the parts supplied in the ARDX kit and then going on to explore extra abilities using parts commonly available from Oomlout and elsewhere. I hope you will be able to join me.

www.bletchleypark.org.uk/

Ian Lang, September 2011