# Electricity & Arduino Primer

We’re going to take a step back and go over the basics of electronics and Arduinos, as a refresher, and to make sure everyone is on level ground.

# Electricity

Electricity is the flow electrons moving through a conductive material.

• Voltage is the difference in charge between two different points (measured in Volts)
• Current is the rate at which electrons flow through a circuit (measures in Amperes or Amps)
• Resistance is the measure of a material’s ability to oppose electrical flow (measured in Ohms)

A good analogy for how these three concepts relate to each other and electricity as a whole, is to envision a system of water, pipes, and resevoirs.

Think of the tank as a battery. As the level of water in the tank decreases, so does the water pressure at the outlet below. Voltage is like the water pressure in this example.

Current, or Amperage, is the rate at which the water flows through the pipes. In this example, the system on the right has less water flowing through it, even through the level of water is the same, because its pipe is narrower. This equates to the system on the right having less Amerage.

By decreasing the water level (Voltage) on the left, and increasing it on the right, each system’s current flow respectively increases and decreases. At some point the flow of water will become equal.

As we discussed above, changing the diameter of the pipe affects the flow of water through the system. This is synonomous with the resistance of a system.

These three properties relate to each other through a formula called Ohm’s Law, which looks like this:

V (Voltage) = I (Current) * R (Resistance)

# Arduino

Arduino is a combination of a hardware microcontroller and software programming language used to control circuits and sense the environment.

This is the top of the Arduino Uno, the most popular variant of the board. It’s split into a few main sections:

• USB B Port
• DC Barrel Jack
• Power Pins
• Digital Pins

You usually program the device by writing up code in the Arduino IDE, and upload it to the board via the USB port. Power may be supplied to the Arduino by the USB port, but may also be supplied via the barrel jack or Vin/GND pins in the power section of the device.

Analog sensors can be read by doing an `analogRead()` on one of the ANALOG IN pins, and digital sensors can be read with a `digitalRead()` on one of the DIGITAL pins. The DIGITAL pins may also be used to provide electricity to a circuit.

Special pins (denoted by a ~ next to their number) can provide Pulse-Width Modulation, or PWM, to whatever is connected. This allows a digital pin to act as an analog out, giving more variation than just HIGH or LOW. A PWMd LED, for example, can seemingly vary its brightness.

# Sensors, Switches, and Other Components

There are a bunch of different electronic components you should become familiar with. We’ll go over a few here.

Analog sensors (like a potentiometer, flex sensor, or photoresistor) can be read simply by asking for their voltage values in the Arduino. Inclusion in a circuit is usually as easy as just simply connecting them with no extra components required. Some sensors are not analog (pulse and ultrasonic distance, for example), and must be activated and read from using a protocol specific to the sensor.

Switches and Buttons stop or redirect the flow of electricity in a circuit.

Actuators like motors and solenoids do work by releasing energy in a controlled way.

Transistors are essentially switches, except they are activated with electricity instead of a mechanical action. They exist in two main forms (at least with what we’ll be using): Bipolar Junction Transistors (BJTs) and MOSFETs. BJTs are controlled by a change of current (which means a resistor must be used to control the exact amount of current applied), and MOSFETs are controlled by a change of Voltage (no resistor required).

There are two types of BJTs — NPNs and PNPs. In diagrams, the arrows for both are on the Emitters. On an NPN, the arrow is Not Pointing In. On an NPN transistor, the Emitter is connected to Ground, Collector is connected to the Current source you want to pass through, and the Base is connected to the Current source you want to use to control the Transistor. In PNP transistors, the Emitter and Collector are switched.

MOSFETs are very similar, except their pins are named Source, Drain, and Gate. In an N-Channel MOSFET, the Source is connected to ground, the Drain is connected to your circuit, and the Gate is given Voltage to switch the MOSFET on or off. In P-Channel MOSFETS, the Source and Drain are switched. In diagrams, the arrows are always on the Gate. On N-Channel MOSFETS, the arrow points in, and on P-Channels it points out.

## DC Motors

DC Motors are a very simple type of motor that turns at varying speeds depending on how much voltage is supplied.

They are extremely simple to use. Simply connect one wire to power, and the other wire to ground to make the motor spin one way. Flip the wires around to make it spin the other way. You can determine what Voltage to use by looking at the motor’s datasheet.

These motors, for example, have a suggested Voltage of 4.5V DC, and will pull 250mA of current at max load.

### Exercise 1.1: Using a Transistor to Control a DC Motor

1. Connect your motor to a Power Supply, and give it 4.5V to see it spin. Flip the wires around the other way to see it spin backwards
2. Design (draw or make in Fritzing) and build a circuit using an N-Channel MOSFET to power that motor via a Power Supply, and control it via an Arduino (you should use the Blink example as a starting point)

## Servos

Servos are a little more complicated than regular DC motors. While generally not as strong, you can control the exact speed at which a servo turns, as well as the direction and position it turns to.

To do this, Servos have three wires: Ground, Power, and Control. The colors vary on different servos, but for the most part, red is usually power, black/brown is usually ground, and white/yellow is usually control.

Servos have some limitations. Most servo motors cannot rotate continuously; cheaper servos can have as little as 90° of rotation. Some servos, however, are known as continuous rotation, and can rotate in full circle indefinitely.

Exact control of a hobby servo is accomplished by sending a series of pulses to the device over the control line. We usually don’t generate our own pulses (they’re difficult to keep track of), and instead rely on existing libraries that have all of the potential commands already written.

### Exercise 1.2: Controlling a Servo with a Potentiometer

1. Connect a servo to your Arduino like so:

1. Open the example code, File > Examples > Servo > Sweep, and upload it to your Arduino

2. Attach a Potentiometer to your system, and use it to control the exact positioning of the Servo (make use of the `map()` command)

#### Sources

Tom Igoe’s Understanding Electricity
Sparkfun’s Voltage, Current, Resistance, and Ohm’s Law and How to Read a Schematic
ITP’s Electricity: the Basics
cmuphyscomp’s physcomp-examples
Oscar Liang’s How to Use MOSFET — Beginner’s Tutorial