Sunday, March 1, 2015

Arduino blocks lesson 2 - switches

Switches are pretty important to learn, they're the main method you'll use to control many projects.

As part of this lesson, we'll learn about schematic diagrams.  Don't be scared, all they are is a way to doodle electronic ideas before building them.

I really recommend doing this lesson on the video. It's largely programming instruction and that comes across best on the video.

Here's the simple circuit that we built at the beginning of the first lesson:
The four lines on the left represent a battery (power source). It looks kind of like a couple of very small cells.  The thing in the upper right, that looks like a large arrow pointing down into a line, is a diode - a device that only lets power flow in one direction.  The circle around it with the arrows leading out of the circle indicates that it is a LIGHT EMITTING diode - or LED.  The squiggly thing in the lower right is a resistor. The electricity has a harder time going through a resistor and the squiggles are intended to represent that.

Here's a schematic of the circuit that we'll build to interface the switches:

The open block on the left represents the Arduino, with pins 13, 3, 2 and GND.  The lines coming out of 3 and 2 go into the schematic representation of a switch - you can see that it's not connected but when you push on the button on top it becomes connected.  When pressed it connects pin 3 or 2 to ground.

The idea here is that you tell the Arduino that pin 3 and 2 are inputs.  When you press a button, it connects that pin to ground and the Arduino can see that it went low, and do whatever you put in the code for that.

There's a problem with this circuit - when the switches are open, pins 2 and 3 aren't connected to anything, so they just float around.  The Arduino might think they're either low or high.  To fix this, we put in what's called a "pull-up resistor" - a resistor that connects each pin to +5 volts making it appear "HIGH" to the Arduino until the switch closes to force it low.  Conveniently, these are used so commonly that the Arduino has them built in, and you can turn them on via programming.  This schematic is the final working version with the pull-up resistors inside the Arduino represented:

For a complete explanation of the code, please watch the video.

You can download the complete BlinkButton sketch by clicking on this link.

No comments: