© Indira Knight 2018
Indira KnightConnecting Arduino to the Webhttps://doi.org/10.1007/978-1-4842-3480-8_1

1. Arduino, Circuits and Components

Indira Knight1 
(1)
London, UK
 

This chapter is an introduction to electronics for Arduino. It will explain how an Arduino is set up and how electricity flows through the circuits and the components. By the end of this chapter you will have used some basic components and created the four base circuits: analog input, analog output, digital input, and digital output.

Arduino

Arduino allows you to create your own electronics projects. It is a collection of open source hardware and software that allows you to attach and control other components to create an electrical circuit. Projects such as an automated plant watering system, a pizza oven, or a remote controlled toy car can be made with an Arduino. When you use an Arduino for a project you need to do the following:
  • Connect components to it.

  • Write a program to control the components.

  • Verify that the program is written correctly.

  • Upload the program to the Arduino.

The Arduino needs to be connected to a computer via a USB port to upload a program to it. Programs for Arduinos are called sketches. Once the sketch is uploaded, it is stored on the microcontroller and will stay there until another sketch is uploaded. Once a new sketch is uploaded the old sketch is no longer available.

Once the sketch is uploaded you can disconnect the Arduino from the computer, and if it is connected to another power source the program will still run.

Note

Once a sketch has been uploaded to an Arduino, it is not represented in the same way you wrote it. You cannot get the sketch back from the Arduino in a form that can be read as the original sketch, so make sure that you save your original code if you want to keep it.

Arduino Hardware

An Arduino board is made up of a number of components, including a microcontroller, digital and analog pins, power pins, resistors, a diode, a capacitor, and an LED. Figure 1-1 shows an Arduino Uno .
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig1_HTML.jpg
Figure 1-1

An Arduino Uno

A Microcontroller has a central processing unit (CPU); it stores the uploaded sketch and processes and directs the commands.

The digital and analog pins are used for sending and receiving digital and analog data.

The Arduino also has a serial interface that allows the Arduino to send data to a computer via the serial port; this is the way we will be sending data to and from a computer in this book.

Electricity

With an Arduino you create an electronic circuit that powers the components attached to it. Wires made of a conductive material connect the components that let electricity flow through them.

Electricity is the movement of electrons through a conductive material. In conductive materials, electrons can move easily between atoms, but in non-conductive materials they can’t.

Atoms are made up of protons, neutrons, and electrons. In the center of the atom are the nucleus, protons and neutrons; electrons are on the outside. Protons have a positive charge and electrons have a negative charge. These two charges are attracted to each other. The electrons are in orbit around the nucleus. In non-conductive materials such as wood or porcelain, it is difficult for the electrons to move; they are tightly bound to the atom. In conductive materials such as copper and other metals, there are electrons that are quite loosely bound to the atoms, so they can move easily. These electrons are on the outer edge of the atom and are called valence electrons.

Electrons move around the circuit from negative to positive. When electricity was first discovered, it was thought that they moved from positive to negative, so by convention the electronic circuits are often drawn from positive to negative, positive to ground (GND). In the circuits in this book the electricity will be flowing in one direction; this is called a direct current (DC), and in an alternating current (AC) the direction changes a certain number of times a second.

To get the electrons in a conductive material to start moving they need a push, and this push is the voltage. Voltage is the difference between higher potential energy and lower potential energy in a circuit. The electrons want to flow from higher potential energy to lower potential energy, from the positive to the ground.

There are a number of ways that voltage is produced. In a battery, it is produced by a chemical reaction. A build-up of electrons is created at the negative end of the battery. When a connection is made to the positive end of the battery, the negative electrons are attracted to the positive, from the higher potential energy to the lower potential energy. This causes them to push the electrons on the wire; the electrons are shunted along the wire.

In Electricity current is the amount of electrons per second that passes a certain point. The current is measured in amps. Each component on the circuit uses up part of the electricity and turns it into another form of energy such as light or sound. The components on the circuit use all the energy in the circuit.

A circuit also has resistance. Resistance is how much the material the current is flowing through slows it down. Resistance is like an obstacle in the way of the current. Resistance is measured in ohms and uses the symbol Ω. Electricity will always choose the easiest way to flow, the path of least resistance. Figure 1-2 is an interpretation of the relationship between voltage, current, and resistance.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig2_HTML.png
Figure 1-2

An interpretation of voltage, current, and resistance

Ohms Law

The physicist and mathematician Georg Simon Ohm discovered a relationship between voltage, current, and resistance; this relationship is called Ohms law. Ohms law says that voltage is equal to amps multiplied by resistance, and it is written as V = I * R where V is volts, I is current, and R is resistance. With this formula you can also find resistance R = V / I and to find current I = V / R, these can be seen in Figure 1-3.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig3_HTML.jpg
Figure 1-3

Ohms law

The components on a circuit use part of the energy in the circuit and turn it into another form of energy such as light or sound. All the energy on a circuit needs to be used by the circuit. If all the energy isn’t used up it needs to go somewhere, or this can cause it to overheat or catch fire. For example, if there is an LED on circuit and it receives too much energy, the light will be very bright and it can blow out. With Ohms law you can calculate the resistance, instead of V = I * R you can find resistance using R = V / I, resistance equals voltage divided by current.

Resistors

Resistors are a crucial component for circuits as they limit the amount of current on the circuit. A resistor has a certain amount of resistance to the current flow. Every component has a maximum amount of current, measured in amps that it can safely use. For example, if a component can take a maximum of 0.023 amps, which is 23 milliamps, and your circuit is receiving 5V (volts), then a 220-ohm resistor will need to be added to the circuit to use the LED safely. The electrical components will use some of the power from the circuit; this is called a voltage drop so this can be taken into account when working out resistance. Figure 1-4 shows an example of how this is worked out with Ohms law, with different voltages and current.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig4_HTML.png
Figure 1-4

The formulae to find the resistance needed for a circuit

When you get a component, it should also have a data sheet, which may be online. This will give you information about the voltage drop and the maximum amps. This will allow you to work out what resistor you need for your circuit.

Resistors have a value measured in ohms; they have a color code to show that value. The value of the resistor tells you what amount of current it will dissipate.

Electronic Circuit Diagrams

Circuit diagrams visually describe a circuit. There are sets of icons that are used for showing components on electronic circuits. In this book I won’t be using electronic circuit diagrams to show the circuits used in the projects but will use images of the Arduino and the components. Figure 1-5 gives you an idea of what a circuit diagram looks like. It is the circuit diagram for an LED.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig5_HTML.jpg
Figure 1-5

A circuit diagram for an LED with a resistor

Figure 1-6 shows some of the icons that can be used in a circuit diagram.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig6_HTML.jpg
Figure 1-6

Some circuit diagram icons

Arduino Software

Arduino has its own programming language; it is a set of C and C++ functions. Arduino programs are called sketches, and they have a .ion extension. Arduino has its own integrated development environment (IDE) that has an editor and other tools to help you write and upload the code.

Downloading and Setting Up the Arduino IDE

You can install the Arduino IDE on your computer. The Arduino IDE is available online and easy to download and install; you can follow the instructions below:
  1. 1.
     
  2. 2.

    The section “Download the Arduino IDE” contains links for the Mac and PC.

     
For Macs:
  1. 1.

    Click the link Mac OS X 10.7 Lion or newer and choose JUST DOWNLOAD, or CONTRIBUTE & DOWNLOAD; both buttons are underneath the picture.

     
  2. 2.

    Unzip the downloaded file.

     
  3. 3.

    The Arduino icon will appear, just click on it to open the IDE.

     
For PC:
  1. 1.

    Click the link Windows Installer or Windows ZIP file for non-admin install depending on the Admin rights you have on your computer. Choose JUST DOWNLOAD, or CONTRIBUTE & DOWNLOAD.

     
  2. 2.

    Unzip the download file.

     
  3. 3.

    You should be able to open the IDE with the icon.

     
When you open the IDE a new sketch should open. Figure 1-7 is an example of an edit window.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig7_HTML.jpg
Figure 1-7

An Arduino IDE edit window

The sketch will contain two functions, setup and loop. There is a tick icon at the top of the page. This is pressed to verify that your code is written correctly. If there are any problems you will get a message in the console in red. The arrow icon is clicked when you want to upload the sketch to your Arduino. The console will show you messages connected to your sketch. It will show you any errors in your code and information when the sketch is verified and uploaded.

Connecting an Arduino to a Computer

You will need a USB 2.0 type B cable to connect an Arduino Uno to your computer. The USB will be used to send data to and from the Arduino as well as powering it. Different types of Arduinos will use different types of cable.

Once you’ve opened the IDE by double-clicking on the icon and connected the Arduino to your computer with the USB, you need to check the tools menu to see that the Arduino Uno is listed as the board and which port it is attached to. In the menu go to Tools/Board, and check that the board says “Arduino/Genuino Uno”; if it doesn’t, pick the Uno from the drop-down menu.

Ports

You will connect your Arduino to your computer by one of its USB ports; these ports have a number and in the Arduino IDE's tool menu, you need to check the port drop-down to choose the port.

Have a look in the Tools/Port menu to make sure that the USB port is being picked up. It will look slightly different depending which USB port you have plugged the Arduino into and if you are on a Mac or a PC. On a Mac it should say something like “dev/cu.usbmodem621 (Arduino/Genuino Uno)”. On a PC it will say something like “COM4 (Arduino/Genuino Uno).”

Write a Sketch

An Arduino has a built-in LED so the easiest sketch to write is one that controls this and makes it blink. It is the first sketch most people will write and is so common that the Arduino you have might already have it installed when you get it.

The Arduino IDE has a number of example sketches and blink is one of them. In the IDE, if you go to File/Examples/01.Basics you will see the blink sketch. You can either open it from there or copy it from the code in Listing 1-1. You will need to save it before you upload it.

void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
Listing 1-1

blink.ino

The Code Explained

Whenever you open a new sketch you will always be given the setup and the loop functions. The setup function is called once when the program is first run. The loop function will keep looping and carrying out the commands in it while the Arduino has power. blink.ino is using the LED on the Arduino and controlling it. This LED is at digital pin 13 on the Arduino, so in the setup you use the pinMode function to say that pin 13 is being used as an output, and it will light up.

The loop has a digitalWrite function that tells pin 13 if it should be high or low (on or off). The delay function pauses the loop for a bit; otherwise it would just run onto the next line of code once it had finished the previous line. The delay is in milliseconds. Table 1-1 explains blink.ino in more detail.
Table 1-1

blink.ino explained

void setup() {

       pinMode(13, OUTPUT);

}

setup is a function. Functions can return values, in this language you need to declare what it will return when you write the function. Both setup() and loop() don’t return anything so the void keyword goes before them. A function is made up of the function name followed by parentheses. The parentheses can be empty or can contain arguments. The argument is passed to the function when the function is called. The curly braces enclose the code for the function.

pinMode(13, OUTPUT);

The Arduino library comes with a number of functions that you can use. pinMode is one of them and digitalWrite is another. When you use a function, it is called calling a function. To call a function you write the function name followed by parentheses. If the function is expecting arguments, they are put inside the parentheses. If it’s not they are left empty. The call to the function is finished with a semicolon. The semicolon lets the program compiler know that it is the end of the call or the command.

You can verify the code to check that it is syntactically correct, then upload it to the Arduino. To verify it you press the tick arrow at the top of the code, and to upload it you click on the arrow icon. These are both shown in Figure 1-7.

Note The Arduino IDE is very sensitive to syntax errors, if you forget a “;” at the end of a command, or write something in lower case when it should be in upper case you will get an error. The IDE is pretty good at letting you know where the error is and they are normally easy to fix.

The Breadboard

The breadboard is used in electronics for prototyping; it is a way to attach components to an Arduino without soldering. It is made of plastic and has a series of holes in it for the pins of the components and for wires. They commonly have two strips of holes down either side for power and ground. Inside the breadboard are strips of metal that are conductive. The wires and pins connect with these strips of metal to make a circuit. They can come in different sizes. Figure 1-8 shows a breadboard.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig8_HTML.jpg
Figure 1-8

A Breadboard

Cables

The Arduino starter kit comes with a number of cables you can use in most of the projects to make a circuit between the Arduino and components using a breadboard. There are some components that have pins that are hard to fit into a breadboard. In Chapter 10 when you make a game controller, you may need to use cables with a different head. There three types of cables: male to male, female to male, and female to female. They are shown in Figure 1-9.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig9_HTML.jpg
Figure 1-9

Cables

Digital and Analog

On an Arduino you can use digital input, digital output, analog input, and analog output. A digital input or output can have one of two states, on or off (high or low). The analog input or output can be between 0 and 1023 when 5V is being used. The following exercises in this chapter show examples of digital output and input, and analog input and output.

Caution

you must unplug your Arduino when you are connecting components. While it is connected to your computer, it has electricity running through it, which could cause an electric shock. If there is too much power for a component, it can pop or explode so you don’t want to be too close if that happens.

Digital Output

A component used for digital output receives a HIGH signal for on and a LOW signal for off. The code in Listing 1-1 uses digital output. In this exercise you will use the same code as in Listing 1-1, blink.ino, which is an LED attached to the Arduino. For this you will need:
  • 1 x Arduino Uno

  • 1 x LED

  • 1 x 220 ohm resistor

The components are shown in Figure 1-10 and the setup of the components is shown in Figure 1-11. Make sure you have unplugged your Arduino from the computer or any power source before attaching the components. The long leg of the Arduino is positive, and the short leg is negative.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig10_HTML.jpg
Figure 1-10

Components for the digital output exercise: 1. Breadboard, 2. LED, 3. 220 Ohm resistor, 4. Arduino Uno

../images/453258_1_En_1_Chapter/453258_1_En_1_Fig11_HTML.jpg
Figure 1-11

Setup for the components for the digital output exercise

Plug your Arduino back into your computer with the USB. If the last program you uploaded was the blink.ino, then you should see the LED blinking. If not, upload the blink.ino again.

Analog Output

Analog output and input produce a range of numbers that go up and down in sequence. On an Arduino some of the digital pins have a “~” symbol next to them. These pins are used for analog output and use PWM (pulse width modulation).

Pulse Width Modulation

PWM is used to simulate an analog output with digital pins. A digital signal can be on or off, and it sends a pulse for on. PWM simulates an analog system using the digital signal by changing the length of the pulse; it’s “on” time to simulate pulses between 5V and 0V. Figure 1-12 shows the pulse width to simulate different voltages.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig12_HTML.jpg
Figure 1-12

Pulse width modulation

Analog Output

As the analog signal can produce a range of numbers, you can do things gradually. In this exercise an LED will fade up slowly before turning off then fading up again. The components for this exercise are the same as Figure 1-10. Make sure the Arduino is unplugged, and then set up the components as shown in Figure 1-13. The LED is connected to digital pin 9, which has a ~ next to it.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig13_HTML.jpg
Figure 1-13

Setup for analog output

Create a new ino sketch, I called mine chapter_01_1.ino, and copy the code from Listing 1-2 into it.

int analogOutPin = 9;
int outputValue = 0;
void setup() {
  pinMode(analogOutPin, OUTPUT);
}
void loop() {
  if (outputValue >= 40){
     outputValue = 0;
  } else {
     outputValue = outputValue + 1;
  }
  analogWrite(analogOutPin, outputValue);
  delay(200);
}
Listing 1-2

chapter_01_1.ino

The Code Explained

To get the LED to fade up, you need to give it a value that increases in each loop. There are a number of new programming concepts in this code. Don’t worry too much if you don’t understand them all yet, as the next few chapters go into programming in more detail. Table 1-2 explains the code in chapter_01_1.ino in more detail.
Table 1-2

chapter_01_1.ino explained

int analogOutPin = 9;

Pin 9 will be used for the LED; it is common practice to store this number in a variable that is used throughout the program. This makes it easier to see what the number represents throughout the program and also allows you to change the pin number once in the code if you decide to use a different pin number.

int outputValue = 0;

A variable holds the value for the LED.

if (outputValue >= 40){

       outputValue = 0;

} else {

       outputValue = outputValue + 1;

}

An if else statement checks if something is true; if it is it does one thing and if not it does another. In this case it checks if the value of the variable outPutValue is greater or equal to 40; if it is it makes the variable contain the value 0, which turns the LED off, and if not it increases it by 1, turning the brightness on the LED up.

analogWrite(analogOutPin, outputValue );

The analogWrite function has two arguments: the pin number and a value, and in this case the value in outputValue is sent to the component attached to pin 9. In this case it is an LED, and this will change the brightness of the LED.

Upload the sketch to the Arduino; you should see the LED increase in brightness then go off.

Digital Input

A good circuit to show a digital input is a switch button. The switch button is either up or down, and it is in one of two states, pressed or not pressed. It brings in another concept called Input Pullup.

There is a problem for an Arduino with a switch. When a switch is open, it does not complete a circuit, and there is no voltage so the Arduino doesn’t know what the input is; it could be 0 or it could be 1. As it doesn’t know you can get strange results, it creates noise as the input value is unknown and it tries to put something in. This problem is solved with pullup resistors; it sets a voltage when the switch is open.

Pullup resistors are built into the Arduino and can be accessed when using the pinMode() function by setting it to INPUT_PULLUP instead of just INPUT. The pin will read HIGH when the switch is open and LOW when it is pressed.

Digital Input

For this exercise, you will use a button to switch on and off the LED on the Arduino. For it you will need:
  • 1 x Arduino Uno

  • 1 x switch button

Remember to disconnect the Arduino from your computer when you are changing components. The switch is attached to the breadboard. One pin is attached to ground the other to digital pin 2. Figure 1-14 shows the components needed and Figure 1-15 shows the setup of the Arduino.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig14_HTML.jpg
Figure 1-14

Components for the digital input exercise: 1. Breadboard, 2. Switch, 3. Arduino Uno

../images/453258_1_En_1_Chapter/453258_1_En_1_Fig15_HTML.jpg
Figure 1-15

Setup for the components for the digital input exercise

Create a new sketch in the Arduino IDE, I called mine chapter_01_2, and copy in the code from Listing 1-3. Verify and upload the code to the Arduino. When you press the button the LED on the Arduino should light.

int buttonInput = 2;
int LEDOutput = 13;
void setup() {
pinMode(buttonInput, INPUT_PULLUP);
  pinMode(LEDOutput, OUTPUT);
}
void loop() {
  int sensorVal = digitalRead(buttonInput);
  if (sensorVal == HIGH) {
     digitalWrite(13, LOW);
  } else {
     digitalWrite(13, HIGH);
  }
}
Listing 1-3

chapter_01_2.ino

The Code ExplainedTable 1-3 explains the code for chapter_01_2.ino in more detail.
Table 1-3

chapter_01_2.ino explained

pinMode(buttonInput, INPUT_PULLUP);

You need to use INPUT_PULLUP as the second argument in the pinMode() function for the button switch.

int sensorVal = digitalRead(buttonInput);

The value of the switch is read into a variable on each loop.

if (sensorVal == HIGH) {

      digitalWrite(LEDOutput, LOW);

} else {

      digitalWrite(LEDOutput, HIGH);

}

An if/else statement checks if the switch is HIGH or LOW. Using the pullup means that the button’s logic is reversed. If it’s HIGH it means it’s up and so the LED is off. When it is LOW it is being pressed, which switched the LED on.

Analog Input

Analog input is used with components such as photoresitors and potentiometers, components that give varying values. An Arduino Uno can register values between 0 and 5 volts; with this you can get an analog input value between 0 and 1023. An analog input sends a signal voltage. When the signal voltage is received it is checked against an internal reference. An example is illustrated in Figure 1-16.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig16_HTML.png
Figure 1-16

An illustration of the analog input process

When a signal voltage is received it is tested against the internal reference at multiple points on the line. For example, it checks if the input is greater than 0; if not, it checks if it’s greater than the next number on the reference and keeps checking until it is. That point becomes the input number.

Analog Input

This exercise uses a potentiometer as an analog input. The potentiometer turns LED on the Arduino on and off when it is turned about halfway.

The components needed for this exercise are the following:
  • 1 x Arduino Uno

  • 1 x potentiometer

Figure 1-17 shows the components and Figure 1-18 shows the setup for the Arduino.
../images/453258_1_En_1_Chapter/453258_1_En_1_Fig17_HTML.jpg
Figure 1-17

Components for the analog input exercise: 1. Breadboard, 2. Potentiometer, 3. Arduino Uno

../images/453258_1_En_1_Chapter/453258_1_En_1_Fig18_HTML.jpg
Figure 1-18

Setup for the components for the analog input exercise

Open a new sketch. I called it chapter_01_3; and then copy the code from Listing 1-4.

int pinAnalogInput  =  A0;
int LEDOutput = 13;
int valueLight =  0;
void setup() {
  pinMode(LEDOutput, OUTPUT);
}
void loop() {
  valueLight =  analogRead(pinAnalogInput);
  if (valueLight < 500) {
    digitalWrite(LEDOutput, LOW);
  } else {
    digitalWrite(LEDOutput, HIGH);
  }
  delay(500);
}
Listing 1-4

chapter_01_3.ino

Verify the sketch and plug the USB back into your computer to upload the sketch to the Arduino. Now when you turn the potentiometer just past halfway, the LED on the Arduino should come off and on.

The code is very similar to the previous sketches in this chapter. The main difference is the variable for the analog pin int pinAnalogInput = A0; The analog input goes through pin A0;

Summary

This chapter was a basic introduction to the Arduino. It looked at how a circuit works and the analog and digital input and output. These are the basic blocks for Arduino that will be built on through the book. The next chapter will get you started with JavaScript and building a web server that will be able to receive data sent from an Arduino.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.117.109