Arduino 101 - What is an Arduino and How to upload code to one.

What is an Arduino.

Arduino

An Arduino is a cheap realtime computer. It’s cheap because you can buy one for $4 to $20. It’s realtime because the only code that runs on it is the code your write.

What are pins?

Pins are pieces of metal connected to the Arduino. There are two main kinds of pins, output and input. Notice the pins are label. Pins 0 to 13 and pins A0 to A5 are the ones we can control.

Arduino Pins

Arduino Pins

Output Pins

Output pins allow you to control whether a pin has electricity or does not have electricity. They are used for controlling servos, motors, leds, and much more.

Arduino Output Pins

Input Pins

Input pins allow you to sense whether electricity is passing through a pin. They are used for motion sensors, humidity sensor, photo sensors and much more.

Arduino Input Pins

Reset Button

The reset button is the big red button in the corner of the Arduino. It’s used to restart the Arduino and will rerun the code that is already on it.

Arduino Reset

RX and TX LEDS

When the RX led is lit, the arduino is recieving a message. When the TX led is lit, the Arduino is sending a message.

Arduino TX And RX

Built in LED

Whenever electricity passes through pin 13 the built in led lights up.

Arduino Built in led

Brain

Arduino Brain

Arduino IDE

1) Download and install the arduino ide.

2) Open the Arduino IDE. Got ot Ardunio -> Preferences.

arduino preference menu

3) Check display line numbers and feel free to increase the font size.

arduino preference

Coding

What is a function

A function is a way you wrap a lines of code into a single reusable line of code. For example let’s say that you need to code to see if your arduino is sensing something, and that takes 5 lines of code. You could wrap those 5 lines of code into a function, and execute those lines with just one line of code. This is called “calling a function”.

The advantage is that you can do this multiple times in your project, thus reducing the amount of code you write. The other advantage is that Arduino IDE provides many built in functions that all you have to do is call.

Anatomy of a function

Example Function

void blue() {
  // ... line of code
  // ... line of code
}

The first word of a function is a the return type. Functions don’t have to return anything. When this happens you write void.

The second word is the name of the function. Function can have letters and numbers as names, but no spaces. The name of our example function is blue.

The parentheses “()” are where you could put arguments in the function. Arguments are named pieces of data that you can pass into a function. We’ll go over that later.

The you have the curly braces “{}”. What in between the curly braces are the lines of code that will be executed once that function is called.

Important note, just because you write a function does not mean that it will execute. You have to call the function in order for it to execute. If we wanted to run the blue function in the code we would do it by writing this.

blue();

The other important note is every line minus curly braces should have a semicolon, “;”, at the end of the code.

Setup and Loop function

The Arduino code has 2 functions, a setup and a loop function. The setup function will run once while the loop functions over and over again.

Prebuilt functions we’ll be using

pinMode

pinMode is used to tell the Arduino the pin type of a pin. The first argument pinMode takes in the pin number, and the second argument is the pin type. The pin types can be OUTPUT, or INPUT. There are other pin types that we’ll go over in later blog posts.

pinMode(7, OUTPUT); // This will make pin 7 an output pin.

digitalWrite

digitalWrite is used to turn on and off electricity for output pins. The first argument it takes is the pin number and second argument it can take is HIGH or LOW. HIGH will turn on electricity and low will turn it off.

digitalWrite(13, HIGH); // This will turn electricity on in pin 13

delay

delay is used to make the arduino pause for a number of milliseconds. The only argument it takes is the number of milliseconds to pause the Arduino. 1000 milliseconds = 1 second.

delay(1000); // This will pause the arduino for one second.

Writing the Code

  1. In the setup function write pinMode(13, OUTPUT);
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {

}

In the loop the loop function write / copy past this.

void setup() {
  pinMode(13, OUTPUT); // Pin 13 is an OUTPUT PIN
}

void loop() {
  digitalWrite(13, HIGH); // Turn pin 13 on
  delay(1000); // wait 1 second
  digitalWrite(13, LOW); // turn pin 13 off
  delay(1000); // wait 1 second

}

Upload the code

1) Go to tools -> port and make sure your arduino usb port is selected.

arduino usb

2) Go to tools -> board and make sure your the type of arduino board you are using is selected.

arduino board

3) Click the check box button on the top left of the Arduino IDE. This button will make sure the code you wrote compiles. Next click the upload button, it’s next to the check button and looks like an arrow. The upload button will upload the code to the Arduino.

arduino board

Challenges

  • Make the internal LED blink every 1/2 second
  • Make the internal LED be on for 3 seconds and off for 1 second
  • Make the internal LED be on all the time.

Cheat Sheet

// Used to tell the Arduino the pin type
pinMode(PIN_NUMBER, OUTPUT_INPUT); 
// Replace "PIN_NUMBER" with the pin number you want to control.  Replace OUTPUT_INPUT with OUTPUT to make in an OUTPUT pin or INPUT to make it an input pin.

// Used to turn on and off electricity for output pins
digitalWrite(PIN_NUMBER, HIGH_LOW);
// Replace "PIN_NUMBER" with the pin number you want to control.  Replace HIGH_LOW with HIGH to turn on electricity or LOW to turn off electricity.

// Used to make the arduino pause for a number of milliseconds
delay(NUMBER_OF_MILLISECONDS);
// Replace NUMBER_OF_MILLISECONDS with the number of milliseconds you want the arduino to pause.  1000 milliseconds = 1 seconds.

Coding Notes

  • Setup function only runs once and is always executed before the loop has ran.
  • Loop function runs over and over again.
  • The code inside the curly braces, “{}” is what is execute.
  • void mean that the block of code does not return anything.

Final Code

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}