My Introduction
First, I would like to thank 0x00sec for providing such a great community. I would also like to thank Arrogant Bastard, Sierra Nevada and Deathwish Coffee for providing the sustenance necessary for starting and completing the following series. Not only should you get drunk while completing these projects but you should consume the correct amount of coffee as well. It’s part of a well balanced diet (which includes ramen of course). You may also need bic lighters and your choice of tobacco, pre rolled cigarettes (like Marlboro) and my favorite, Zig Zag rolling papers.
I hold a degree in Information Systems (CIS), and am not an electrician or mechanical engineer. I have been creating software since 1998, so about 20 years. I run my own SEO and web development business and am creating this series to help this community grow and to help you, the reader, grow. I was creating software before the internet was blessed with PHP and when Oracle was still 8i. I am an old drunken tattooed programmer, so excuse the language if you’re a christian, excuse the drinking if you’re a teetotaler, and most importantly remember that I am always open to criticism, hate and suggestions.
Arduino Series Introduction
The Arduino has made it possible for hundreds of thousands of people to create gadgets they normally otherwise wouldn’t have been able to build. The Arduino is a micro controller that allows you to assemble and create just about anything you can dream up. Arduinos can be bought for dirt cheap online (ebay, amazon) and there are even resources to create your own Arduino using schematics provided by the Arduino team.
I will be using Ubuntu (Xenial) as the operating system of my choice. I will not be covering anything under the Windows operating system. I will also not be using any of the online Arduino compilers/Uploaders such as the one at arduino.cc. I will also use the words method and function interchangeably, as they mean the same thing. Some language (such as C#) call them methods and some languages prefer to call them functions (like PHP).
This series will walk you through creating projects with the Arduino. The projects will be more advanced the further we dive into the Arduino. This series will require you to have certain sensors and and hardware required to complete the projects. I will provide links to websites to purchase the sensors and hardware, and of course, I will keep price in mind. Most sensors and “shields” for the Arduino are inexpensive. Some of the sensors and components we will be integrating will be:
- LCD screens
- Heat and Temperature
- Light
- Flame
- Buttons
- WiFi
- Sound detectors
- Lasers
- Cameras
- Dot Matrixes
- etc etc
Pre-requisites
- It is assumed that you have a basic understanding of the C programming language. If you don’t know anything about programming, you may not make it very far in this series.
- You will need a laptop/desktop computer capable of running the Arduino IDE.
- I will be using an Arduino Uno R3. You can use another type of Arduino provided you understand the pinouts and voltages required for the upcoming projects/articles.
- A breadboard
- Jumper wires
- An Arduino board
Getting Started
First, let’s install the IDE and get it up and connected to the board.
sudo apt-get -y install arduino
Once it is installed, open the IDE and connect the Arduino via the USB cable. You will need to make sure the correct board is chosen from the Tools menu.
The Arduino’s code that we will upload is called a sketch. You can find many many free sketches online for all sorts of things. The code is a set of C and C++, so knowing C will help you greatly and YES you must know the basics of C. You could learn along the way but I strongly suggest learning it before continuing this series. So let’s take a look at the Arduino.
- Reset button
- Digital pins
- USB connector
- AT Mega 328P (data sheet)
- DC power connector
The reset button will reset the board, replaying the sketch from the beginning. The digital pins you need to know about right now, (from left to right as pictured) are GND for ground and 13 to 0. We might cover the AREF pin later on (this pin is for Analog reference). The pins on the other side is for Analog, which we will use later as well.
Voltage and Dangers
The Arduino outputs 5 volts. This is indeed what is referred to as “low voltage”. Nothing we do here will involve “mains” voltage, which could kill you. We will be using sensors that could possibly burnt he shit out of you and cause you to go blind momentarily, but I assure you, you will be okay. For example, if your pumping 5 volts into a sensor that requires 1 volt or half a volt and you touch it, you will probably be burned. Be careful and use your head brain.
Your First LED Blink
Our first project will be something very simple, to get you familiar with the coding and uploading process. We will use a very simple LED project, which is normally the first thing you do in any other “hello world” type Arduino project. So you will need an LED for this one, like my red one below. We won’t even need a breadboard for this one.
As you can see, there is one little prong longer than the other. This is what distinguishes the positive from the ground. The bottom one, which is the longer one, is called the anode and the shorter one we are going to ground is the cathode. Some LED’s even have a flat spot on the casing which can help identify which one is which, one side won’t always be longer in some cases.
Take this LED and put the longer end (positive) into pin 13 on your Arduino. Pin 13 is right next to the ground, your LED should be able to fit in both, like below:
Now, we are ready for some easy code. You can go ahead and copy and paste the following code into the IDE.
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13,HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(2000);
}
There are two functions above, setup and loop. The setup function is used to declare variables and is run when the sketch starts. The loop function will, as you may have guessed it, loop over and over again. These functions will be used often and probably in every project we do.
In the setup function we are setting up pin 13 to be used an output, meaning voltage will be output to this pin. You can tell that OUTPUT is a constant, since it’s in all capitals. In the loop function we are sending the HIGH signal to pin 13, waiting 2000ms (2 seconds), sending the LOW signal, then waiting another 2000ms. Once it reached the end it will loop again, over and over again. Once this is uploaded to the board, you should have a fancy blinking light, which isn’t much of anything, but at least you got your “hello world” out of the way.
Once the code is in place, your Arduino is plugged into the USB and you have the correct com selected, upload the sketch to the board. There is a button on the top bar that has an arrow pointing to the right, this is the upload button. When you hover over the button, you should see it’s meaning in the status bar. If the upload fails, you will see error messages at the bottom of the IDE, probably in the color red.
Serial Monitor
The Serial Monitor is a console area where you can write debug stuff too. This window is very helpful for debugging. You can access it by pressing Ctrl+Shift+M. The serial monitor is not active by default. We need to activate it by using the Serial.begin() method. In the setup function is where you need to activate it.
Serial.begin(9600);
When you want to log something to the console (serial monitor) you will use the println method. Here is an example of how to use it with the code we already have in place:
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(13,HIGH);
Serial.println("LED is on");
delay(2000);
digitalWrite(13, LOW);
Serial.println("LED is off");
delay(2000);
}
Remember, you don’t need to save the sketch to upload it. You will need to upload it after you enter new code, the hotkey for that is Ctrl+u, or like earlier, press the upload button.
Breadboards
Breadboards are a place you can “prototype” your electronics before soldering them. We will do most of our work on breadboards so it is important to understand how they work. My breadboard has a sticky back on it (obviously to stick it in place somewhere). I chose not to stick it down since I am usually mobile.
Your First Button
Here is what you will need for this:
- 1 Button
- 1 Breadboard
- 10k Ohm Resister
- 5 Jumper wires
You will notice that there is 2 lines, one red and one blue. The red line has a plus sign (positive) and the blue has a minus sign (for ground). When we power things on the breadboard will be putting the output voltage into the red line.
Above is the button I will be attaching to the breadboard. This can be a pain in the ass depending on how cheap your breadboard is. My breadboard is pretty cheap so it may take some wiggling to get it in the right place and secure. The button has 4 legs (pins) on the bottom.
Our button will have 1 leg fed 5 volts, this leg will have the resistor on it, which we will ground out. We will use another leg to get the button’s state to to connect the circuit to our LED on pin 13. So, just like in our first LED project, go ahead and connect the LED back to pin 13 and the ground, directly onto the Arduino itself.
Now, let’s add the resistor. I cut mine down so it looks nice and neat on the board, and keeps it tight and together. I am not using a 10k ohm, I am using a 220. You can tell by the colored bands on the resistor what it is. You should still use a 10k for best (non buggy) results.
Next, from the pin that says 5v (on the other side of the digital pins) on the analog side, run a jumper wire into the red (positive) side of our breadboard.
Next, connect a jump from GND (ground) to the blue line (negative/ground).
Now, ground off the resistor
Feed our last leg into the digital side of the Arduino in pin 7.
Our button will have two states, not pressed and pressed. Everything should be in place now, go ahead and put the code below in the IDE and upload it to the board.
const int btnPin = 7;
const int led = 13;
int btnState = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(btnPin, INPUT);
}
void loop() {
btnState = digitalRead(btnPin);
if (btnState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Now you can have fun pressing a button to turn on your LED. How fun huh? Yea, not exciting but you gotta start somewhere. The code to me is self explanatory but for shits and giggles, let’s run through it. In the setup function we are setting pin 13 as our output and pin 7 as our input, which is our button. In the main loop we have a conditional that is checking if our button is being pressed or not. If the button is pressed send the HIGH signal (send voltage to the LED), if it’s not pressed, send the LOW signal, turning the LED off. Well, not so much turning it off but not giving it any voltage.
What’s Next
Next in this Arduino series I will show you how to use a 7 segment LED, potentiometers, sound detection and temperature detection. I will also go into reading schematics, writing schematics and using Fritzing.
Remember, you should always drink beer, run with scissors, throw change at rich people and keep coding.