Arduino is an electronic prototyping platform that allows anyone to create a variety of electromechanical devices. This platform includes both software and hardware components. The software part includes the development environment - a program for writing and debugging firmware, a rich set of ready-to-use and convenient libraries, as well as a simplified programming language. The hardware part consists of a wide range of microcontrollers and ready-made modules designed for them. Thanks to this structure, working with Arduino becomes extremely easy and accessible.
Arduino not only teaches programming, electronics, and mechanics, but also serves as a more advanced tool. Based on this platform, you can create truly useful devices - from simple blinkers and weather stations to automation systems, smart homes, CNC machines, and even drones.
Programming on Arduino
When you have a microcontroller board at hand and the development environment installed on your computer, you can start creating your first sketches (firmware). It is important to familiarize yourself with the programming language used on Arduino.
A simplified version of the C++ language with predefined functions is used for programming on Arduino. Like in other C-style programming languages, there are certain rules for writing code. Here are the main ones:
1. A semicolon (;) should be placed after each instruction.
2. Before declaring a function, you need to specify the return data type or void if the function does not return anything.
3. The data type should also be specified before declaring a variable.
4. Comments can be single-line (//) or multi-line (/* ... */).
Additional information about data types, functions, variables, operators, and language constructs can be found on the Arduino programming page. It is not necessary to memorize all this information - you can always refer to the reference manual and check the syntax of a particular function.
Each firmware for Arduino should contain at least two functions: setup() and loop().
The setup() function
The setup() function is executed once at the very beginning after the device is powered on or reset. Typically, in this function, pin modes are set, communication protocols are initialized, connections to external modules are established, and used libraries are configured. If your firmware does not require such actions, the function still needs to be declared. Here is an example of a standard setup() function:
Code: Select all
void setup() {
Serial.begin(9600); // Open a serial connection
pinMode(9, INPUT); // Assign pin 9 as an input
pinMode(13, OUTPUT); // Assign pin 13 as an output
}
The loop() function
The loop() function is executed after setup(). As the name suggests this function will be executed cyclically. For example, the ATmega328 microcontroller, installed in most Arduino boards, will run the loop function about 10,000 times per second (if there are no delays or complex calculations). This provides a lot of opportunities.
Breadboard
You can create simple and complex devices. For convenience, it is recommended to purchase a breadboard and wires. They allow you to avoid soldering and rewiring wires, modules, buttons, and sensors for different projects and debugging. Using a breadboard simplifies and speeds up development. More information about working with a breadboard can be found in this lesson. Here are some types of breadboards:
The First Arduino Project
Let's assemble a simple device using Arduino. We will just connect a tactile button and an LED. The project schematic is as follows:
Please note the additional resistors present in this schematic. One of them is used to limit the current flowing through the LED, and the other is used to ground the button contact. To ensure functionality, a corresponding sketch needs to be written. Let's create a program that will turn on the LED when the button is pressed and turn it off when pressed again. Below is our first sketch:
Code: Select all
// variables with the pins of the connected devices
int switchPin = 8;
int ledPin = 11;
// variables to store the state of the button and the LED
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
// function for debouncing
boolean debounse(boolean last) {
boolean current = digitalRead(switchPin);
if(last != current) {
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop() {
currentButton = debounse(lastButton);
if(lastButton == LOW && currentButton == HIGH) {
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(ledPin, ledOn);
}
Arduino PWM
Pulse-width modulation (PWM) is a method of controlling voltage by varying the signal duration. PWM allows for smooth regulation of the load. An example is smoothly adjusting the brightness of an LED. This is achieved by changing the intervals between low signals, rather than the voltage. The principle of PWM operation is shown in the schematic: Applying PWM signal to an LED results in rapid flickering, invisible to the human eye due to the high frequency. However, video recording can capture these moments if the camera's frame rate does not match the PWM frequency.
Arduino includes a built-in pulse-width modulation (PWM) module. This is only possible on pins supported by the microcontroller. For example, Arduino Uno and Nano offer 6 PWM pins: D3, D5, D6, D9, D10, and D11. Options may vary on other boards, so it is recommended to refer to the specific board's description.
To use PWM in Arduino, the analogWrite() function is used. It takes the pin number and PWM value (from 0 to 255), where 0 represents 0% duty cycle with a high signal, and 255 represents 100%. Here is an example: the LED will smoothly fade in, wait for a second, then fade out smoothly, and so on. The example code is provided below:
Code: Select all
// The LED is connected to pin 11
int ledPin = 11;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 255; i++) {
analogWrite(ledPin, i);
delay(5);
}
delay(1000);
for (int i = 255; i > 0; i--) {
analogWrite(ledPin, i);
delay(5);
}
}
Analog pins, unlike digital ones, can only receive signals and measure the voltage of the incoming signal. The conversion occurs with a 10-bit precision, where 0 corresponds to 0V, and 1023 corresponds to 5V. This allows for measuring voltage with an accuracy of up to 0.005V, which is useful for working with sensors and resistors (such as photoresistors, thermistors).
To work with analog signals on Arduino, the analogRead() function is used. In the example, we connect a photoresistor and create a sketch that reads and outputs the readings to the serial monitor. The circuit includes a pull-down resistor of 10kΩ to suppress noise. The example code is provided below:
Code: Select all
int sensePin = 0; // The pin to which the photoresistor is connected
void setup() {
analogReferense(DEFAULT); // Setting a reference voltage value. This line is optional.
Serial.begin(9600); // Opening the port at a baud rate of 9600
}
void loop() {
Serial.println(analogRead(sensePin)); // Reading the value and outputting it to the port
delay(500); // Delay to limit the amount of values
}