Introduction to the Arduino Series

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:

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.

png

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.

IMG_20180527_012959
IMG_20180527_012959-2

  1. Reset button
  2. Digital pins
  3. USB connector
  4. AT Mega 328P (data sheet)
  5. 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.

IMG_20180527_021727

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:

IMG_20180527_022525

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);
}

png

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

IMG_20180527_032015

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.

IMG_20180527_032641

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.

IMG_20180527_033020

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.

IMG_20180527_040645

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.

IMG_20180527_041000

Next, connect a jump from GND (ground) to the blue line (negative/ground).

IMG_20180527_041238

Now, ground off the resistor

IMG_20180527_041410

Feed our last leg into the digital side of the Arduino in pin 7.

IMG_20180527_041614

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.

21 Likes

FINALLY!!! Something that I understand instead of these weirdos posting RE stuff (RE sucks (jk))…

I’m excited to do more projects with my Arduino Uno. Good job! I will do a more thorougher reading later since I’m busy atm, but I see from a fast scrolling how well in detail this is. Oh btw, @anon79434934 is our hardware dude. If you want to, maybe collaborate with him? With that all being said…

Anyways, ~Cheers!

–Techno Forg–

1 Like

Comprehensive article with an emphasis on those who don’t know hardware all too well.

Am I glad that you started this series so I won’t have to!

2 Likes

Hey man, very nice article. I’ve been using arduinos and atmega328 based hardware for a few years, and just wanted to comment that it’s so much more than hobby stuff… i designed an industrial water pump control system around it, and it’s been up and running for 3+ years without any serious mishaps.
right now working on interfacing the atmega to an esp01 wifi board, dirt cheap and very effective

1 Like

Hi @fxbg,
Nice series which surely helps beginners a lot!
Just wanted to point out that this passage:

if (btnState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

Can be shortened to:

digitalWrite(ledPin, btnState);

elitism off :stuck_out_tongue_winking_eye:

1 Like

@SmartOne, you’re right, for two state buttons like in this tutorial that would be fine, I’m not sure if it would be okay for buttons with a neutral state.

1 Like

Just curious, but what do you mean by buttons with a neutral state(3 states)?
If you retrieve the state with analogRead() there, it still should work as in Arduino C HIGH corresponds to a boolean true.
E.g. bool btnState = analogRead(0) > 400 && analogRead(0) < 800;

The button used in the tutorial only has 2 states, some buttons (switches I guess) have multiple states, but I’ve never used one on the Arduino, I’ll have to get one and check it out.

Also, will this work with the button? (I’m not in the office right now or I’d give it a go)

analogRead(0) > 400

I am glad this topic is getting attention here, I might have to move into more advanced territory sooner than I thought :slight_smile:

i’m diggin this thread. trying to figure out how to follow it.

@dtwozero, What are you having a problem with?

The > 400 was only a guess, you’ll have to read the documentation depending on which button you use :slight_smile:

LOL i’m not having a problem with the content. i was looking for the button to keep me posted on the thread itself.

1 Like

@SmartOne
As far as I know a button is just a button. The button used in the post looks like a push-button or more specifically a momentary push button (because it comes back to the original position when you stop pushing it).

This kind of button are usually switches with a spring. When you push it, pins in both sides of the switch get connected by a small piece of metal. When you release it, the spring push back that piece of metal and the contact disappears.

There are switches with 3 and even more pins. Those, basically connects one of those pins to another, when the switch is actioned. However, there is no real switch state in the sense that you have to read the value from each of the switch pins to verify which one has been activated. Otherwise you have to use a multiplexor or some smart wiring (Charlieplexing for instance).

So what all this means is that, using analogRead will just read the voltage that is the other side of button whatever it is. The voltage will not change because the button is pressed. (Well, it actually will change during the transient state but that is not relevant here).

So for that multi-state button, the closest thing I can think about is a smartphone headset with volume control. In this case you have to read an analogue value (analogRead for Arduino). You connect each button to a ADC (the mic input typically in your phone) through a simple voltage divider. Using different resistor values for each button will produce different voltage in the DAC and effectively lead to code like the one you proposed. You have to chose those resistors so there is some voltage margin between the readings for each button.

Alternatives are using potentiometers or encoders… Even when you could use both as kindof buttons, they are not buttons/switches strictly speaking.

@fxbg nice post. It is very easy to read and gives a very nice overview to let people dive in this world. You really go into the relevant parts for getting started.

My 2 cents: Specifically for push buttons, it is a good idea to use some Debouncing code. In your example it won’t make much of a difference but in the general case is something to keep in mind.

2 Likes

Ah yes, debouncing (which I did not cover), I believe the ohm resistor I used helps with this as well, just for some added extra info for people reading this post.

I’d say that is a Pull-up/down resistor.

1 Like

man you know this job… thanks

This topic was automatically closed after 30 days. New replies are no longer allowed.