This tutorial is meant as an introduction to writing code for Arduino boards using the Arduino IDE. Also, note that the examples herein make use of arduino libraries with C++. This is the best way to introduce yourself to programming Arduino. See my github repository to see a list of several arduino programs I have written.
What is arduino?
Arduino is a microcontroller. It can be programmed to achieve from simple to some complicated tasks. It is majorly used for testing and learning purposes but it can also be used in production. To start programming with arduino we will require either a physical arduino board or a simulation software (I would recommend Tinkercad). If you are using the former, you will also require the Arduino IDE. Head over to Arduino's official website to download the IDE and install for the operating system you are using.
Program structure
There are two main functions used.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Setup
As the name suggests, in this function, this is where you setup stuff. You can, for example, specify whether pins are outputs or inputs. This is also where you specify the screen baud rate. Below is example code which can go into this function.
void setup() {
pinMode(9, INPUT)
}
Loop
This is where you setup tasks that are going to be repeated indefinitely. Remember that Arduino is a microcontroller which can be used in embedded systems, and embedded systems need to continuously take input from the surrounding while producing output when desired. Like a watchman of some sorts. So, the code has to run the whole time.
As an example, lets suppose you want to turn on a red LED after every 10 seconds as an indicator for something such as a prepaid customer interface unit under a certain condition, say the customer units are below 5. The code would need to always be on watch for when the units go below the set point (5) and then produce the output, in this case light in the form of an LED. Here is an example code that would go into the loop function. The below code is extracted from a program producing sound output through a buzzer. Note that passive, dt and dt2
are variables that had been earlier declared.
void loop() {
// Make a tone by turning the buzzer on and off
digitalWrite(passive,HIGH);
delayMicroseconds(dt2);
digitalWrite(passive,LOW);
delayMicroseconds(dt);
}
Variables
These are containers for storing values. The most commonly used variables in Arduino are Integers, floats, strings and Booleans. I should note that the analog pins such as A0 are treated as integers in this case.
Examples
int buzzPin = 8;
float readVal = 125.5;
String msg = "Switched off";
bool switchState = true;
Conditionals
These are used to perform an action under a given condition. The mostly used conditions are the if statements and the switch statements. In our example during the introduction here, If the units are below 5, we would then produce the output in form of an LED light. Below is an example code using if statement.
if(buttonState == 1) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
Loops
The mostly used loops are while and for loops. To loop through a known number of iterations, you would mostly opt for a for loop and while loop otherwise.
While loop
The while loop is common when waiting for user input from the serial monitor for testing purposes. Below is an example for this use case.
// Wait until a value is entered
while (Serial.available() == 0) {
}
For loop
For simplicity’s sake, I am just going to provide an example for this case.
for (i = 0; i<=10; i=i+1) {
digitalWrite(buzzPin, HIGH);
delay(dt);
digitalWrite(buzzPin, LOW);
delay(dt2);
}
Printing values
When I say printing values, I mean to the monitor not some external interface or anything that fancy. This is especially useful for testing and debugging purposes. The printLn
or the print functions are used for this purpose. The difference here is that printLn
produces output on a new line. Below is an example to print a string to the serial monitor.
Serial.println("Enter desired servo angle");
Reading values
There are two main functions used for the purpose of reading analog signals and digital input from pins.
resVal = analogRead(photoPin);
switchVal = digitalRead(sPin);
analogRead()
Used to read analog input from the analog pins or PWM enabled pins.A reading from a potentiometer as example would be an analog input as it would vary the voltage producing some form of wave.
digitalRead()
This is used to read input from digital pins. This can be a 1 or a 0 (HIGH and LOW.) This can be the input from a switch for example.
Writing values to pins
There are also two main functions used to write values to pins.
analogWrite(ledPin,128);
digitalWrite(ledPin, HIGH);
analogWrite()
This function is used to write analog signals to a pin. Electronic components which require a PWM signal for control are a good example where you would use analogWrite().
digitalWrite()
Used for writing a 1 (HIGH) or a 0(LOW) to a digital pin. An example is turning on and off of an LED.