Arduino uno r3 projects for beginners. Arduino: what can be done with it. Projects for the future

This simulator works best in Chrome browser
Let's take a closer look at the Arduino.

Arduino is not a big computer that external circuits can be connected to. Arduino Uno uses Atmega 328P
This is the largest chip on the board. This chip executes programs that are stored in its memory. You can download the program via usb using the Arduino IDE. The USB port also provides power to the arduino.

There is a separate power socket. There are two outputs on the board, labeled 5v and 3.3v, which are needed in order to power various devices. You will also find pins labeled GND, these are ground pins (ground is 0V). The Arduino platform also has 14 digital pins (pins), labeled with numbers from 0 to 13, that connect to external nodes and have two states, high or low (on or off). These contacts can work as outputs or as inputs, i.e. they can either transmit some data and control external devices, or receive data from devices. The following pins on the board are labeled A0-A5. These are analog inputs that can receive data from various sensors. This is especially useful when you need to measure a certain range, such as temperature. The analog inputs have additional functions that can be activated separately.

How to use a breadboard.

The breadboard is there to temporarily connect the parts, to test how the device works before you solder everything together.
All of the following examples are assembled on a breadboard so that you can quickly make changes to the circuit and reuse parts without the hassle of soldering.

The breadboard has rows of holes where you can insert parts and wires. Some of these holes are electrically connected to each other.

The two upper and lower rows are connected in series along the entire board. These rows are used to supply power to the circuit. It can be 5v or 3.3v, but either way, the first thing you need to do is connect 5v and GND to the breadboard as shown. Sometimes these row connections can be interrupted in the middle of the board, then if you need to, you can connect them, as shown in the figure.








The rest of the holes located in the middle of the board are grouped by five holes. They are used to connect circuit parts.


The first thing we will connect to our microcontroller is an LED. The wiring diagram is shown in the picture.

What is the purpose of the resistor in the circuit? In this case, it limits the current that passes through the LED. Each LED is designed for a certain current, and if this current is greater, then the LED will fail. You can find out what value the resistor should be using the ohm law. For those who do not know or have forgotten, Ohm's law says that there is a linear relationship between current and voltage. That is, the more we apply voltage to the resistor, the more current will flow through it.
V=I*R
Where V- voltage across the resistor
I- current through the resistor
R is the resistance to be found.
First, we must find out the voltage across the resistor. Most of the 3mm or 5mm LEDs you will be using have a 3v operating voltage. So, on the resistor we need to pay off 5-3 \u003d 2v.

We then calculate the current passing through the resistor.
Most 3 and 5mm LEDs glow at full brightness at 20mA. A current greater than this can destroy them, and a lesser current will reduce their brightness without causing any harm.

So, we want to turn on the LED in a 5v circuit so that it has a current of 20mA. Since all parts are included in one circuit, the resistor will also have a current of 20mA.
We get
2V=20mA*R
2V=0.02A*R
R = 100 ohm

100 ohms is the minimum resistance, it is better to use a little more, because the LEDs have some variation in characteristics.
In this example, a 220 ohm resistor is used. Only because the author has a lot of them :wink: .

Insert the LED into the holes in the middle of the board so that its long lead is connected to one of the leads of the resistor. Connect the other end of the resistor to 5V, and connect the other end of the LED to GND. The LED should light up.

Please note that there is a difference in how to connect the LED. Current flows from the longer lead to the shorter lead. In the diagram, it can be imagined that the current flows in the direction where the triangle is directed. Try to flip the LED and you will see that it will not glow.

But how you will connect the resistor, there is no difference at all. You can turn it over or try to connect it to a different output of the LED, this will not affect the operation of the circuit. It will still limit the current through the LED.

Anatomy of an Arduino Sketch.

Programs for Arduino are called sketch. They have two main functions. Function setup and function loop
inside this function you will set all the basic settings. Which outputs will work as input or output, which libraries to connect, initialize variables. Function Setup() runs only once during the sketch, when program execution starts.
this is the main function that is executed after setup(). In fact, it is the program itself. This function will run indefinitely until you turn off the power.

Arduino blinking LED



In this example, we'll connect an LED circuit to one of the Arduino's digital pins and turn it on and off with the program, as well as learn a few useful functions.

This feature is used in setup() part of the program and serves to initialize the pins that you will use as input (INPUT) or exit (OUTPUT). You will not be able to read or write data from the pin until you set it accordingly in pinMode. This function has two arguments: pinNumber is the pin number you will use.

mode-sets how the pin will work. At the entrance (INPUT) or exit (OUTPUT). To light the LED, we must give a signal FROM Arduino. To do this, we configure the pin to exit.
- this function is used to set the state (state) pina (pinNumber). There are two main states (generally there are 3 of them), one is HIGH, there will be 5v on the pin, the other is low and the pin will be 0v. So, in order to light the LED, we need to set a high level on the pin connected to the LED HIGH.

Delay. Serves to delay the program for a period specified in ms.
Below is the code that makes the LED blink.
//LED Blink int ledPin = 7;//Arduino pin to which LED is connected void setup() ( pinMode(ledPin, OUTPUT);// set pin as OUTPUT ) void loop() ( digitalWrite(ledPin, HIGH);// turn on the LED delay(1000);// delay 1000 ms (1 sec) digitalWrite(ledPin, LOW);// Turn off the LED delay(1000);// wait 1 sec )

Small explanations on the code.
Lines that start with "//" are comments Arduino ignores them.
All commands end with a semicolon, if you forget them, you will get an error message.

ledPin is a variable. Variables are used in programs to store values. In this example, the variable ledPin the value 7 is assigned, this is the Arduino pin number. When the Arduino in the program meets a line with a variable ledPin, it will use the value we specified earlier.
So record pinMode(ledPin, OUTPUT) similar to the entry pinMode(7, OUTPUT).
But in the first case, you just need to change the variable and it will change in each line where it is used, and in the second case, in order to change the variable, you will have to make changes with handles in each command.

The first line indicates the type of the variable. When programming an Arduino, it is important to always declare the type of variables. As long as you know that INT declares negative and positive numbers.
Below is a sketch simulation. Press start to see how the circuit works.

As expected, the LED turns off and on after one second. Try changing the delay to see how it works.

Control of multiple LEDs.

In this example, you will learn how to control multiple LEDs. To do this, install 3 more LEDs on the board and connect them to the Arduino resistors and pins as shown below.

In order to turn on and off the LEDs in turn, you need to write a program similar to this:
//Multi LED Blink int led1Pin = 4; int led2Pin = 5; intled3Pin = 6; int led4Pin = 7; void setup() ( //set pins as OUTPUT pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(led4Pin, OUTPUT); ) void loop() ( digitalWrite(led1Pin, HIGH );//light LED delay(1000);//delay 1 sec digitalWrite(led1Pin, LOW);//turn off LED delay(1000);//delay 1 sec //do the same for the other 3 LEDs digitalWrite(led2Pin , HIGH); // turn on the LED delay(1000);// delay 1 sec digitalWrite(led2Pin, LOW);// turn off the LED delay(1000);// delay 1 sec digitalWrite(led3Pin, HIGH);// turn on the LED delay(1000);// delay 1 sec digitalWrite(led3Pin, LOW);// turn off LED delay(1000);// delay 1 sec digitalWrite(led4Pin, HIGH);// turn on LED delay(1000);// delay 1 sec digitalWrite(led4Pin, LOW);//turn off LED delay(1000);//delay 1 sec )

This program will work fine, but it's not the smartest solution. The code needs to be changed. In order for the program to work over and over again, we will apply a construction called .
Loops are useful when you need to repeat the same action several times. In the code above, we repeat the lines

DigitalWrite(led4Pin, HIGH); delay(1000); digitalWrite(led4Pin, LOW); delay(1000);
full sketch code in attachment (downloads: 1260)

LED brightness adjustment

Sometimes you will need to change the brightness of the LEDs in the program. This can be done with the command analogWrite() . This command turns the LED on and off so quickly that the eye does not see this flicker. If the LED is on half the time and off half the time, then visually it will seem that it glows at half its brightness. This is called Pulse Width Modulation (PWM or PWM in English). PWM is used quite often, since it can be used to control an "analog" component using a digital code. Not all Arduino pins are suitable for this purpose. Only those conclusions near which such a designation is drawn " ~ ". You will see it next to pins 3,5,6,9,10,11.
Connect one of your LEDs to one of the PWM pins (the author's pin is 9). Now run the LED blinking sketch, but first change the command digitalWrite() on analogWrite(). analogWrite() has two arguments: the first is the pin number, and the second is the PWM value (0-255), in relation to LEDs, this will be their brightness, and for electric motors, the rotation speed. Below is an example code for different LED brightness.
//Change the brightness of the LED int ledPin = 9;//an LED is connected to this pin void setup() ( pinMode(ledPin, OUTPUT);// initialization of the pin to the output ) void loop() ( analogWrite(ledPin, 255);// full brightness (255/255 = 1) delay(1000);// pause 1 sec digitalWrite(ledPin, LOW);// turn off LED delay(1000);// pause 1 sec analogWrite(ledPin, 191);// brightness by 3/4 (191/255 ~= 0.75) delay(1000);//pause 1 sec digitalWrite(ledPin, LOW);//turn off LED delay(1000);//pause 1 sec analogWrite(ledPin, 127); //half brightness (127/255 ~= 0.5) delay(1000);//pause 1s digitalWrite(ledPin, LOW);//turn off LED delay(1000);//pause 1s analogWrite(ledPin, 63); // quarter brightness (63/255 ~= 0.25) delay(1000);// pause 1 sec digitalWrite(ledPin, LOW);// turn off LED delay(1000);// pause 1 sec )

Try changing the PWM value in the command analogWrite() to see how this affects brightness.
Next, you will learn how to adjust the brightness smoothly from full to zero. You can, of course, copy a piece of code 255 times
analogWrite(ledPin, brightness); delay(5);//short delay brightness = brightness + 1;
But, you understand - it will not be practical. The best way to do this is to use the FOR loop we used earlier.
The following example uses two loops, one to decrease brightness from 255 to 0
for (int brightness=0;brightness=0;brightness--)( analogWrite(ledPin,brightness); delay(5); )
delay(5) used to slow down the fade in and fade out speed 5*256=1280ms=1.28sec.)
The first line uses " brightness-" to decrease the brightness value by 1 each time the loop is repeated. Note that the loop will run until brightness >=0.Replacing the sign > on the sign >= we have included 0 in the brightness range. This sketch is modeled below. //smoothly change the brightness int ledPin = 9;//an LED is connected to this pin void setup() ( pinMode(ledPin, OUTPUT);// initialization of the output pin ) void loop() ( //smoothly increase the brightness (0 to 255 ) for (int brightness=0;brightness=0;brightness--)( analogWrite(ledPin,brightness); delay(5); ) delay(1000);//wait 1 sec //slowly decrease brightness (255 to 0) for (int brightness=255;brightness>=0;brightness--)( analogWrite(ledPin,brightness); delay(5); ) delay(1000);//wait 1 sec ) )
It doesn't look very good, but the idea is clear.

RGB LED and Arduino

An RGB LED is actually three LEDs of different colors in one package.

By turning on different LEDs with different brightness, you can combine and get different colors. For an Arduino with 256 gradations you get 256^3=16581375 possible colors. In reality, of course, there will be fewer of them.
The LED we will be using as the common cathode. Those. all three LEDs are structurally connected by cathodes to one output. We will connect this pin to the GND pin. The remaining outputs, through limiting resistors, must be connected to the PWM outputs. The author used pins 9-11. Thus, it will be possible to control each LED separately. The first sketch shows how to turn on each LED individually.



//RGB LED - test //pin connections int red = 9; int green = 10; int blue = 11; void setup()( pinMode(red, OUTPUT); pinMode(blue, OUTPUT); pinMode(green, OUTPUT); ) void loop()( //turn on/off the red LED digitalWrite(red, HIGH); delay(500) ; digitalWrite(red, LOW); delay(500); //turn on/off the green LED digitalWrite(green, HIGH); delay(500); digitalWrite(green, LOW); delay(500); //turn on/off the blue LED digitalWrite(blue, HIGH); delay(500); digitalWrite(blue, LOW); delay(500); )

The following example uses the commands analogWrite() and to get different random brightness values ​​for the LEDs. You will see different colors changing randomly.
//RGB LED - random colors //pin connections int red = 9; int green = 10; int blue = 11; void setup()( pinMode(red, OUTPUT); pinMode(blue, OUTPUT); pinMode(green, OUTPUT); ) void loop()( //pick a random color analogWrite(red, random(256)); analogWrite( blue, random(256)); analogWrite(green, random(256)); delay(1000);//wait one second )

Random(256)-returns a random number between 0 and 255.
In the attached file, a sketch that will demonstrate the smooth transitions of colors from red to green, then to blue, red, green, etc. (downloads: 348)
The sketch example works, but there is a lot of repetitive code. You can simplify the code by writing your own helper function that will smoothly change from one color to another.
Here's what it will look like: (downloads: 385)
Let's look at the function definition piece by piece. The function is called fader and has two arguments. Each argument is separated by a comma and has the type declared on the first line of the function definition: void fader(int color1, int color2). You can see that both arguments are declared as int, and they are named color1 And color2 as condition variables to define the function. Void means that the function does not return any values, it just executes commands. If you had to write a function that returned the result of a multiplication, it would look like this:
int multiplier(int number1, int number2)( int product = number1*number2; return product; )
Notice how we declared the Type int as return type instead of
void.
Inside the function there are commands that you already used in the previous sketch, only the pin numbers were replaced with color1 And color2. Function is called fader, its arguments are calculated as color1=red And color2 = green. The archive contains a complete sketch using functions (downloads: 288)

Button

The following sketch will use a button with normally open contacts, without latching.


This means that while the button is not pressed, no current flows through it, and after releasing, the button returns to its original position.
In the circuit, in addition to the button, a resistor is used. In this case, it does not limit the current, but "pulls up" the button to 0v (GND). Those. until the button is pressed, the Arduino pin to which it is connected will be low. The resistor used in the 10 kΩ circuit.


//determine the button press int buttonPin = 7; void setup()( pinMode(buttonPin, INPUT);//initialize pin to input Serial.begin(9600);//initialize serial port ) void loop()( if (digitalRead(buttonPin)==HIGH)(//if the button is pressed Serial.println("pressed"); // print "pressed" ) else ( Serial.println("unpressed");// else "unpressed" ) )
There are several new commands in this sketch.
-This command accepts the value High (high level) and low (low level) of the output that we are checking. Previously, in setup(), this output must be configured as an input.
; //where buttonPin is the pin number where the button is connected.
The serial port allows the Arduino to send messages to the computer while the controller itself is executing the program. This is useful for debugging a program, sending messages to other devices or applications. To enable data transfer through the serial port (another name for UART or USART), you need to initialize it in setup ()

Serial.begin() has only one argument - this is the data transfer rate between the Arduino and the computer.
The sketch uses a command to display a message on the screen in the Arduino IDE (Tools >> Serial Monitor).
- the design allows you to control the progress of the program by combining several checks in one place.
If(if) digitalRead returns HIGH, then the word "pressed" is displayed on the monitor. Else(otherwise) the word "pressed" is displayed on the monitor. Now you can try turning the LED on and off at the touch of a button.
//button press detection with LED output int buttonPin = 7; int ledPin = 8; void setup()( pinMode(buttonPin, INPUT);//this time we will set button pin as INPUT pinMode(ledPin, OUTPUT); Serial.begin(9600); ) void loop()( if (digitalRead(buttonPin)= =HIGH)( digitalWrite(ledPin,HIGH); Serial.println("pressed"); ) else ( digitalWrite(ledPin,LOW); Serial.println("unpressed"); ) )

Analog input.

analogRead allows you to read data from one of the Arduino analog pins and outputs a value in the range from 0 (0V) to 1023 (5V). If the voltage at the analog input is 2.5V, then 2.5 / 5 * 1023 = 512 will be printed
analogRead has only one argument - This is the number of the analog input (A0-A5). The following sketch shows the code for reading voltage from a potentiometer. To do this, connect a variable resistor, with the extreme terminals to the 5V and GND pins, and the middle terminal to the A0 input.

Run the following code and see in the serial monitor how the values ​​change depending on the turn of the resistor knob.
//analog input int potPin = A0;//the center pin of the potentiometer is connected to this pin void setup()( //analog pin is enabled as input by default, so initialization is not needed Serial.begin(9600); ) void loop()( int potVal = analogRead(potPin);//potVal is a number between 0 and 1023 Serial.println(potVal); )
The following sketch combines the button press sketch and the LED brightness control sketch. The LED will turn on from the button, and the potentiometer will control the brightness of the glow.
//button press detection with LED output and variable intensity int buttonPin = 7; int ledPin = 9; int potPin = A0; void setup()( pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); ) void loop()( if (digitalRead(buttonPin)==HIGH)(//if button pressed int analogVal = analogRead(potPin); int scaledVal = map(analogVal, 0, 1023, 0, 255); analogWrite(ledPin, scaledVal); // turn on led with intensity set by pot Serial. println("pressed"); ) else ( digitalWrite(ledPin, LOW);//turn off if button is not pressed Serial.println("unpressed"); ) )

Delivery of new homemade products to the post office

Receive a selection of new homemade products by mail. No spam, just useful ideas!

In this article, I decided to put together a complete step-by-step guide for Arduino beginners. We will analyze what an arduino is, what you need to start learning, where to download and how to install and configure the programming environment, how it works and how to use the programming language, and much more that is necessary to create full-fledged complex devices based on the family of these microcontrollers.

Here I will try to give a concise minimum so that you understand the principles of working with Arduino. For a more complete immersion in the world of programmable microcontrollers, pay attention to other sections and articles of this site. I will leave links to other materials on this site for a more detailed study of some aspects.

What is Arduino and what is it for?

Arduino is an electronic kit that allows anyone to create a variety of electro-mechanical devices. Arduino consists of software and hardware. The software part includes a development environment (a program for writing and debugging firmware), a lot of ready-made and convenient libraries, and a simplified programming language. The hardware part includes a large line of microcontrollers and ready-made modules for them. Thanks to this, working with Arduino is very easy!

With the help of arduino, you can learn programming, electrical engineering and mechanics. But this is not just a training constructor. Based on it, you can make really useful devices.
Starting from simple flashing lights, weather stations, automation systems and ending with smart home systems, CNC machines and unmanned aerial vehicles. The possibilities are not limited even by your imagination, because there are a huge number of instructions and ideas for implementation.

Arduino Starter Kit

In order to start learning Arduino, you need to get the microcontroller board itself and additional details. It is best to purchase an Arduino starter kit, but you can also pick up everything you need on your own. I advise choosing a set because it's easier and often cheaper. Here are links to the best kits and individual parts that are sure to come in handy for you to study:

Basic arduino kit for beginners:Buy
A large set for training and first projects:Buy
A set of additional sensors and modules:Buy
Arduino Uno is the most basic and convenient model from the line:Buy
Solderless breadboard for easy learning and prototyping:Buy
A set of wires with convenient connectors:Buy
LED kit:Buy
Resistor kit:Buy
Buttons:Buy
Potentiometers:Buy

Arduino IDE development environment

To write, debug and upload firmware, you need to download and install the Arduino IDE. This is a very simple and convenient program. On my site, I have already described the process of downloading, installing and configuring the development environment. Therefore, here I will simply leave links to the latest version of the program and to

Version Windows MacOS X linux
1.8.2

Arduino programming language

When you have a microcontroller board on hand and a development environment is installed on your computer, you can start writing your first sketches (firmware). To do this, you need to familiarize yourself with the programming language.

Arduino programming uses a simplified version of the C++ language with predefined functions. As in other C-like programming languages, there are a number of rules for writing code. Here are the most basic ones:

  • Each statement must be followed by a semicolon (;)
  • Before declaring a function, you must specify the data type returned by the function, or void if the function does not return a value.
  • It is also necessary to specify the data type before declaring a variable.
  • Comments are indicated by: // Inline and /* block */

You can learn more about data types, functions, variables, operators and language constructs on the page. You do not need to memorize and memorize all this information. You can always go to the reference and see the syntax of a particular function.

All firmware for Arduino must contain at least 2 functions. They are setup() and loop().

setup function

In order for everything to work, we need to write a sketch. Let's make it so that the LED lights up after pressing the button, and after the next press it goes out. Here is our first sketch:

// variables with pins of connected devices int switchPin = 8; int ledPin = 11; // variables for storing the state of the button and LED boolean lastButton = LOW; boolean currentButton = LOW; boolean ledOn = false; void setup() ( pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); ) // debounce function 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); )

// variables with pins of connected devices

int switchPin = 8 ;

int ledPin = 11 ;

// variables to store the state of the button and LED

boolean lastButton = LOW ;

boolean currentButton = LOW ;

boolean ledOn = false ;

void setup()(

pinMode (switchPin , INPUT ) ;

pinMode (ledPin , OUTPUT ) ;

// debounce function

boolean debounce (boolean last ) (

boolean current = digitalRead(switchPin) ;

if (last != current ) (

delay(5) ;

current = digitalRead(switchPin) ;

return current ;

void loop()(

currentButton = debounce(lastButton) ;

if (lastButton == LOW && currentButton == HIGH ) (

ledOn = ! ledOn ;

lastButton = currentButton ;

digitalWrite(ledPin , ledOn ) ;

In this sketch, I created an additional debounce function to suppress contact bounce. About the bounce of contacts is on my website. Be sure to check out this material.

PWM Arduino

Pulse Width Modulation (PWM) is the process of controlling voltage by taking a duty cycle of a signal. That is, using PWM, we can smoothly control the load. For example, you can smoothly change the brightness of an LED, but this change in brightness is obtained not by reducing the voltage, but by increasing the low signal intervals. The principle of operation of PWM is shown in this diagram:

When we apply PWM to an LED, it starts to quickly turn on and off. The human eye is unable to see this because the frequency is too high. But when shooting video, you will most likely see moments when the LED is off. This will happen provided that the frame rate of the camera is not a multiple of the PWM frequency.

The Arduino has a built-in pulse width modulator. You can use PWM only on those pins that are supported by the microcontroller. For example, Arduino Uno and Nano have 6 PWM outputs: these are pins D3, D5, D6, D9, D10 and D11. Pins may vary on other boards. You can find a description of the board you are interested in in

To use PWM, the Arduino has a function. It takes as arguments the pin number and the PWM value from 0 to 255. 0 is 0% high signal fill, and 255 is 100%. Let's write a simple sketch as an example. Let's make the LED light up smoothly, wait one second and fade away just as smoothly, and so on ad infinitum. Here is an example of using this function:

// LED 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); ) )

// LED 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 -- ) (

The Arduino is very popular among all DIY enthusiasts. It should be introduced to them and those who have never heard of him.

What is an Arduino?

How can you briefly describe Arduino? The best words would be: Arduino is a tool with which you can create various electronic devices. In fact, this is a real hardware computing platform for universal use. It can be used both to build simple circuits and to implement fairly complex projects.

The constructor is based on its hardware, which is an I / O board. The board is programmed using languages ​​based on C/C++. They were named, respectively, Processing/Wiring. From group C, they inherited extreme simplicity, due to which they are mastered very quickly by any person, and applying knowledge in practice is not a rather significant problem. To give you an idea of ​​how easy it is to work, it is often said that the Arduino is for beginner wizards. Even children can deal with Arduino boards.

What can be collected on it?

The use of Arduino is quite diverse, it can be used both for the simplest examples that will be recommended at the end of the article, and for quite complex mechanisms, including manipulators, robots or production machines. Some craftsmen manage to make tablets, phones, home surveillance and security systems, smart home systems or just computers based on such systems. Arduino projects for beginners, which even someone with no experience can get started with, are at the end of the article. They can even be used to create primitive virtual reality systems. All thanks to the rather versatile hardware and the possibilities that Arduino programming provides.

Where to buy components?

Components made in Italy are considered original. But the price of such kits is not low. Therefore, a number of companies or even individuals make artisanal Arduino-compatible devices and components, which are jokingly called production clones. When buying such clones, it is impossible to say with certainty that they will work, but the desire to save money takes its toll.

Components can be purchased either as part of kits or individually. There are even pre-made kits to assemble cars, helicopters with different types of controls or ships. A set like the one pictured above, made in China, will cost $49.

More about hardware

The Arduino board is a simple AVR microcontroller that was flashed with a bootloader and has the minimum required USB-UART port. There are still important components, but within the limits of article it will be better to stop only on these two components.

First, about the microcontroller, a mechanism built on a single circuit, in which the developed program is located. The program can be influenced by pressing buttons, receiving signals from the components of creation (resistors, transistors, sensors, etc.), etc. Moreover, sensors can be very different in their purpose: lighting, acceleration, temperature, distance, pressure, obstacles etc. As display devices, simple parts can be used, from LEDs and tweeters to complex devices, such as graphic displays. As motors, valves, relays, servos, electromagnets and many others are considered, which are listed for a very, very long time. With something from these lists, MK works directly, using connecting wires. Some mechanisms require adapters. But if you start designing, it will be difficult for you to break away. Now let's talk about Arduino programming.

Learn more about the board programming process

A program that is already ready to work on a microcontroller is called firmware. There can be both one project and Arduino projects, so it would be desirable to store each firmware in a separate folder in order to speed up the process of finding the right files. It is stitched onto the MK crystal by means of specialized devices: programmers. And here "Arduino" has one advantage - it does not need a programmer. Everything is done so that programming Arduino for beginners is not difficult. The written code can be downloaded to the MK via a USB cable. This advantage is achieved not by some programmer already built in advance, but by a special firmware - a bootloader. The bootloader is a special program that runs immediately after connection and listens to see if there are any commands, whether to flash the chip, whether there are Arduino projects or not. There are several very attractive advantages of using a bootloader:

  1. Using only one communication channel, which does not require additional time costs. So, Arduino projects do not require you to connect many different wires, and there was confusion when using them. One USB cable is enough for successful operation.
  2. Protection from crooked hands. Bringing the microcontroller to the state of a brick using direct firmware is quite easy, you don’t need to strain much. When working with a bootloader, you can’t get to potentially dangerous settings (with the help of a development program, of course, otherwise you can break everything). Therefore, Arduino for beginners is intended not only from the point of view that it is understandable and convenient, it will also allow you to avoid unwanted financial expenses associated with the inexperience of the person working with them.

Projects to start

When you have acquired a kit, a soldering iron, rosin and solder, you should not immediately sculpt very complex structures. Of course, you can blind them, but the chance of success in Arduino for beginners is quite low for complex projects. For training and "stuffing" your hands, you can try to implement a few simpler ideas that will help you deal with the interaction and operation of the Arduino. As such first steps in working with Arduino for beginners, we can advise you to consider:

  1. Create one that will work thanks to "Arduino".
  2. Connecting a separate button to the Arduino. In this case, it is possible to make the button able to control the glow of the LED from point No. 1.
  3. Potentiometer connection.
  4. Servo control.
  5. Connection and operation with three-color LED.
  6. Connection of the piezoelectric element.
  7. Photoresistor connection.
  8. Connecting a motion sensor and signals about its operation.
  9. Connecting a humidity or temperature sensor.

Projects for the future

It is unlikely that you are interested in "Arduino" in order to connect individual LEDs. Most likely, you are attracted by the opportunity to create your own car, or a flying turntable. Such projects are difficult to implement, they require a lot of time and perseverance, but by completing them, you will get what you wanted: valuable Arduino design experience for beginners.

A series of articles and tutorials with amateur radio experiments on Arduino for beginners. This is such an amateur radio toy-designer, from which, without a soldering iron, etching of printed circuit boards, and the like, any kettle in electronics can assemble a full-fledged working device, suitable for both professional prototyping and amateur experiments when studying electronics.


The Arduino board for is intended primarily for teaching beginner radio amateurs the basics of programming microcontrollers and creating microcontroller devices with their own hands without serious theoretical training. The Arduino development environment allows you to compile and load ready-made program code into the board's memory. Moreover, downloading the code is extremely simple.

Arduino where to start for a beginner

First of all, to work with the Arduino board, a beginner electronics engineer needs to download the Arduino development program, it consists of a built-in text editor in which we work with program code, a message area, a text output window (console), a toolbar with buttons for frequently used commands, and multiple menus. To download its programs and communicate, this program is connected to the Arduino board via a standard USB cable.


Code written in the Arduino environment is called sketch. It is written in a text editor that has special tools for inserting / cutting, replacing / searching for text. During saving and exporting, the message area (see the figure in the first tutorial for beginners, just below) shows explanations, and errors may also be displayed. The console shows Arduino messages, including full error reports and other useful information. The toolbar buttons allow you to check and record a sketch, open, create and save it, open serial bus monitoring, and much more.

So, let's move on to the first Arduino circuit lesson for beginner electronics engineers.

For the convenience of beginners, the Arduino UNO controller already has a resistance and an LED connected to pin 13 of the connector, so we don’t need any external radio elements in the first experiment.


By loading the code, Arduino allows our program to participate in the initialization of the system. To do this, we indicate to the microcontroller the commands that it will execute at the time of the initial boot and then completely forget about them (i.e. these commands will be executed by the Arduino only once at startup). And it is for this purpose that in our code we allocate a block in which these commands are stored. void setup(), or rather in that space inside the curly braces of this function, see the programming sketch.

Don't forget the curly braces! Losing even one of them will make the whole sketch completely inoperative. But do not put extra brackets either, because an error will also occur.

Code download:
Sketch with comments and explanations in file 001-1_mig-led.ino

Function void loop() this is where we put commands that will run as long as the Arduino is powered on. Starting execution from the first command, Arduino will reach the very end and immediately jump to the beginning to repeat the same sequence. And so an infinite number of times, as long as the board receives power. At its core, void loop is the main function, the entry point to Arduino.


Function delay(1000) delays program processing by 1000 milliseconds. All this goes in an eternal cycle loop().

The main conclusion after the perception of our first program on Arduino: With the help of the void loop and void setup functions, we pass our instructions to the microcontroller. Everything inside the setup block will be executed only once. The contents of the loop module will be repeated in a loop as long as the Arduino remains on.

In the previous program, there was a one-second delay between the LED turning on and off. There was one big minus in the simplest code of the novice arduinist used above. To hold the pause between turning on and off the LED in one second, we applied the function delay() and therefore at that moment the controller is not capable of executing other commands in the main function loop(). Correction of the code in the function loop() presented below solves this problem.

Instead of setting the value to HIGH and then to LOW, we get the value of ledPin and invert it. Suppose if it was HIGH, then it will become LOW, etc.

Second arduino code variant to control led Here:

Then you can replace the function delay(). Instead, it's better to use the function millis(). It returns the number of milliseconds elapsed since the program started. The function will overflow after approximately 50 days of running the code.

A similar function is micros(), which returns the number of microseconds that have elapsed since the code was run. The function will return to zero after 70 minutes of running the program.

Of course, this will add a few lines of code to our sketch, but it will definitely make you a more experienced programmer and increase the potential of your Arduino. To do this, you just need to learn how to use the millis function.

It should be clearly understood that the simplest delay function suspends the execution of the entire Arduino program, making it unable to perform any tasks during this period of time. Instead of pausing our entire programs, we can count how much time has passed before the completion of the action. This, nicely, is implemented using the millis() function. To make everything easy to understand, we will consider the following option for blinking an LED without a time delay.

The beginning of this program is the same as any other standard Arduino sketch.


This example uses two Arduino digital I/Os. The LED is connected to pin 8, which is configured as OUTPUT. A button is connected to 9 through, which is configured as INPUT. When we press the button, pin 9 is set to HIGH, and the program switches pin 8 to HIGH, thus turning on the LED. Releasing the button resets the ninth output to the LOW state. The code then switches pin 8 to LOW, turning off the indicator light.

To control the five LEDs, we will use various manipulations with the Arduino ports. To do this, we will directly write data to the Arduino ports, this will allow you to set the values ​​\u200b\u200bfor the LEDs using just one function.

Arduino UNO has three ports: B(digital inputs/outputs 8 to 13); C(analogue inputs); D(Digital I/Os 0 to 7)

Each port controls three registers. The first DDR specifies whether the pin will be an input or an output. With the help of the second PORT register, you can set the pin to the HIGH or LOW state. With the help of the third, you can read information about the state of the Arduino legs, if they work as an input.

For the operation of the circuit, we will use port B. To do this, set all the pins of the port as digital outputs. Port B has 6 pins in total. The DDRB register bits must be set in "1" if the pin will be used as an OUTPUT, and in "0" if we plan to use the pin as an input (INPUT). Port bits are numbered from 0 to 7, but do not always have all 8 pins

Let's say: DDRB = B00111110;// set port B pins 1 to 5 as outputs and 0 as input.

In our running lights circuit, we use five outputs: DDRB = B00011111; // set port B pins 0 to 4 as outputs.

To write data to port B, you need to use the PORTB register. You can light the first LED using the control command: PORTB=B00000001;, first and fourth LED: PORTB=B00001001 and so on

There are two binary shift operators: left and right. The left shift operator causes all data bits to move to the left, respectively, the right shift operator moves them to the right.

Example:

varA = 1; // 00000001
varA = 1 varA = 1 varA = 1

Now let's return to the source code of our program. We need to introduce two variables: upDown will include the values ​​where to move - up or down, and the second cylon will indicate which Led to light.

Structurally, such an LED has one common output and three outputs for each color. Below is a diagram of connecting an RGB LED to an Arduino board with a common cathode. All resistors used in the connection circuit must be of the same value from 220-270 ohms.


To connect with a common cathode, the connection scheme for a three-color led will be almost the same, except that the common pin will not be connected to ground (gnd on the device), but to the +5 volt terminal. Outputs Red, green and blue in both cases are connected to the digital outputs of the controller 9, 10 and 11.

Connect an external LED to the ninth pin of the Arduino UNO through a 220 ohm resistance. To smoothly control the brightness of the latter, we use the function analogWrite(). It provides a PWM signal output to the controller leg. And the team pinMode() calling is not required. Because analogWrite(pin, value) includes two parameters: pin - pin number for output, value - value from 0 to 255.

Code:
/*
A training example for a beginner arduino, reveals the possibilities of the analogWrite () command to implement the Fade effect of the LED
*/
int brightness = 0; // LED brightness
int fadeAmount = 5; // brightness step
unsigned long currentTime;
unsigned long loopTime;

Void setup() (
pinMode(9, OUTPUT); // set pin 9 as output
currentTime = millis();
loopTime = currentTime;
}

void loop() (
currentTime = millis();
if(currentTime >= (loopTime + 20))(
analogWrite(9, brightness); // set value on pin 9

Brightness = brightness + fadeAmount; // add a step of changing the brightness, which will be set in the next cycle

// if min. or max. values, then we go in the opposite direction (reverse):
if (brightness == 0 || brightness == 255) (
fadeAmount = -fadeAmount ;
}
loopTime = currentTime;
}
}

Working Arduino with an encoder

The encoder is designed to convert the angle of rotation into an electrical signal. From it we get two signals (A and B), which are opposite in phase. In this tutorial, we will use the SparkFun COM-09117 encoder, which has twelve positions per revolution (each position is exactly 30°). The figure below clearly shows how the output A and B depend on each other when the encoder moves clockwise or counterclockwise.

If signal A goes from positive to zero, we read the value of output B. If output B is in a positive state at that time, then the encoder moves in the clockwise direction, if B outputs a zero level, then the encoder moves in the opposite direction. By reading both outputs, we are able to calculate the direction of rotation with the help of a microcontroller, and by counting the pulses from the A output of the encoder, the angle of rotation.

If necessary, you can use the frequency calculation to determine how fast the encoder is rotating.

Using the encoder in our tutorial, we will control the brightness of the LED using the PWM output. To read data from the encoder, we will use a method based on software timers, which we have already discussed.

Considering the fact that in the fastest case, we can turn the encoder knob 180° in 1/10 of a second, this will be 6 pulses in 1/10 of a second, or 60 pulses in one second.

In reality, it is not possible to rotate faster. Since we need to keep track of all half cycles, the frequency should be around 120 Hertz. To be completely sure, let's take 200 Hz.

Since, in this case, we use a mechanical encoder, contact bounce is possible, and the low frequency perfectly filters out such a bounce.


According to the software timer signals, it is necessary to constantly compare the current value of the output A of the encoder with the previous value. If the state changes from positive to zero, then we poll the status of output B. Depending on the result of the status poll, we increment or decrement the counter of the LED brightness value. The program code with a time interval of about 5 ms (200 Hz) is shown below:

Arduino beginner code:
/*
** Encoder
** An encoder from Sparkfun is used to control the brightness of the LED
*/

int brightness = 120; // LED brightness, starting at half
int fadeAmount = 10; // brightness step
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 12; // pin 12
const int pin_B = 11; // pin 11
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
void setup()(
// declare pin 9 to be an output:
pinMode(9, OUTPUT); // set pin 9 as output
pinMode(pin_A, INPUT);
pinMode(pin_B, INPUT);
currentTime = millis();
loopTime = currentTime;
}
void loop() (
currentTime = millis();
if(currentTime >= (loopTime + 5))( // check states every 5ms (frequency 200Hz)
encoder_A = digitalRead(pin_A); // read encoder output A state
encoder_B = digitalRead(pin_B); // encoder output B
if((!encoder_A) && (encoder_A_prev))( // if state changes from positive to zero
if(encoder_B) (
// output B is positive, so rotation is clockwise
// increase the brightness of the glow, no more than 255
if(brightness + fadeAmount )
else(
// output B is in the zero state, so the rotation is counterclockwise
// reduce brightness, but not below zero
if(brightness - fadeAmount >= 0) brightness -= fadeAmount;
}

}
encoder_A_prev = encoder_A; // save the value of A for the next loop

analogWrite(9, brightness); // set the brightness to the ninth pin

LoopTime = currentTime;
}
}

In this beginner example, we will look at working with a piezo buzzer to generate sounds. To do this, take a piezoelectric transducer that allows you to generate sound waves in the frequency range of 20 Hz - 20 kHz.

This is such an amateur radio design where LEDs are located throughout the volume. With this scheme, you can generate various lighting and animation effects. Complex schemes can even display various three-dimensional words. In other words, this is an elementary surround monitor

The servo is the main element in the construction of various radio-controlled models, and its control with the controller is simple and convenient.


The control program is simple and intuitive. It starts with connecting a file containing all the necessary commands to control the servo. Next, we create a servo object, such as servoMain. The next setup() function, in which we specify that the servo is connected to the ninth output of the controller.

Code:
/*
Arduino Servo
*/
#include
Servo main; // Servo Object

Void setup()
{
servoMain.attach(9); // Servo connected to pin 9
}

Void loop()
{
servoMain.write(45); // Rotate the servo to the left by 45°
delay(2000); // Wait 2000 milliseconds (2 seconds)
servoMain.write(0); // Rotate servo left by 0°
delay(1000); // Pause 1s.

delay(1500); // Wait 1.5s.
servoMain.write(135); // Rotate the servo to the right by 135°
delay(3000); // Pause 3 s.
servoMain.write(180); // Rotate the servo to the right by 180°
delay(1000); // Wait 1s.
servoMain.write(90); // Rotate the servo 90°. center position
delay(5000); // Pause 5 s.
}

In the main function loop(), we give commands to the servomotor, with pauses between them.

Arduino counter circuit on a 7-segment indicator

This simple Arduino project for beginners is to create a counter circuit on a common cathode 7-segment display. The code below allows you to start counting from 0 to 9 when you press the button.

Seven-segment indicator - is a combination of 8 LEDs (the last one is responsible for the dot) with a common cathode, which can be turned on in the desired sequence so that they create numbers. It should be noted that in this circuit, see the figure below, pins 3 and 8 are reserved for the cathode.


On the right is a table showing the mapping between Arduino pins and LED indicator pins.

Code for this project:

byte numbers = (
B11111100, B01100000, B11011010, B11110010, B01100110,
B10110110, B10111110, B11100000, B11111110, B11100110
};
void setup()(
for(int i = 2; i pinMode(i, OUTPUT);
}
pinMode(9, INPUT);
}
int counter = 0;
bool go_by_switch = true;
int last_input_value = LOW;
void loop() (
if(go_by_switch) (
int switch_input_value = digitalRead(9);
if(last_input_value == LOW && switch_input_value == HIGH) (

}
last_input_value = switch_input_value;
) else (
delay(500);
counter = (counter + 1) % 10;
}
writeNumber(counter);
}

Void writeNumber(int number) (
if(number 9) (
return;
}
bytemask = numbers;
byte currentPinMask = B10000000;
for(int i = 2; i if(mask & currentPinMask) digitalWrite(i,HIGH);
else digitalWrite(i,LOW);
currentPinMask = currentPinMask >> 1;
}
}

You can significantly expand the potential of Arduino boards with the help of additional modules that can be connected to the PIN outputs of almost any device. Consider the most popular and interesting expansion modules or, as they are also called, shields.

Latency in Arduino plays a very important role. Without them, even the simplest example of Blink, which blinks an LED after a specified period of time, cannot work. But most novice programmers know little about time delays and only use Arduino delay without knowing the side effects of this command. In this article, I will talk in detail about temporary functions and the features of their use in the Arduino IDE development environment.

The Arduino has several different commands that are responsible for working with time and pauses:

  • delay()
  • delayMicroseconds()
  • millis()
  • micros()

They differ in accuracy and have their own characteristics that should be considered when writing code.

Using the arduino delay function

Syntax

Arduino delay is the simplest command and most often used by beginners. In fact, it is a delay that pauses the program for the number of milliseconds indicated in brackets. (There are 1000 milliseconds in one second.) The maximum value can be 4294967295 ms, which is approximately equal to 50 days. Let's look at a simple example that illustrates how this command works.

Void setup() ( pinMode(13, OUTPUT); ) void loop() ( digitalWrite(13, HIGH); // signal pin 13 high delay(10000); // pause 10000ms or 10 seconds digitalWrite13, LOW); // send a low signal to pin 13 delay(10000); // pause 10000ms or 10 seconds )

In method setup we prescribe that pin 13 will be used as an output. In the main part of the program, first a high signal is applied to the pin, then we make a delay of 10 seconds. At this time, the program seems to be suspended. Then a low signal is given and again a delay and everything starts all over again. As a result, we get that the pin is alternately supplied, then 5 V, then 0.

You need to clearly understand that during the pause using delay, the program is suspended, the application will not receive any data from the sensors. This is the biggest disadvantage of using the Arduino delay function. You can get around this limitation using interrupts, but we will talk about this in a separate article.

Delay example with blinking LED

An example diagram to illustrate how the delay function works.
You can build a circuit with an LED and a resistor. Then we get a standard example - blinking an LED. To do this, on the pin, which we designated as the output, you need to connect the LED with a positive contact. We connect the free leg of the LED through a resistor of approximately 220 ohms (a little more can be) to ground. You can determine the polarity if you look at its insides. The large cup inside is connected to the minus, and the small leg is connected to the plus. If your LED is new, then you can determine the polarity by the length of the leads: a long leg is a plus, a short leg is a minus.

delayMicroseconds function

This function is a complete analog of delay, except that its units are not milliseconds, but microseconds (1000000 microseconds in 1 second). The maximum value will be 16383, which is equal to 16 milliseconds. The resolution is 4, meaning the number will always be a multiple of four. An example snippet would look like this:

DigitalWrite(2, HIGH); // send pin 2 high delayMicroseconds(16383); // pause 16383µs digitalWrite(2, LOW); // send a low signal to pin 2 delayMicroseconds(16383); // pause 16383µs

The problem with delayMicroseconds is exactly the same as with delay - these functions completely "hang" the program and it literally freezes for a while. At this time, it is impossible to work with ports, read information from sensors and perform mathematical operations. For flashers, this option is fine, but experienced users do not use it for large projects, since such failures are not needed there. Therefore, it is much better to use the functions described below.

millis function instead of delay

The millis() function will allow you to perform a delay without delay on the arduino, thereby bypassing the shortcomings of the previous methods. The maximum value of the millis parameter is the same as for the delay function (4294967295ms or 50 days).

Using millis, we do not stop the execution of the entire sketch, but simply indicate how long the arduino should simply “bypass” exactly the block of code that we want to pause. Unlike delay millis by itself does not stop anything. This command simply returns to us from the built-in timer of the microcontroller the number of milliseconds that have passed since the start. With each call to loop We ourselves measure the time elapsed since the last call to our code, and if the time difference is less than the desired pause, then we ignore the code. As soon as the difference becomes more than the desired pause, we execute the code, get the current time using the same millis and remember it - this time will be the new reference point. In the next cycle, the countdown will already be from the new point and we will again ignore the code until the new difference between millis and our previously stored value reaches the desired pause again.

Delaying without delay with millis requires more code, but it can blink the LED and pause the sketch without stopping the system.

Here is an example that clearly illustrates the work of the command:

unsigned long timing; // Variable for storing the reference point void setup() ( Serial.begin(9600); ) void loop() ( /* This is where the execution of delay() analogue begins. Calculate the difference between the current moment and the previously saved reference point. If the difference is greater than If not, do nothing */ if (millis() - timing > 10000)( // Instead of 10000, substitute the pause value you need timing = millis(); Serial. println ("10 seconds") ; ) )

First, we introduce a timing variable, which will store the number of milliseconds. By default, the value of the variable is 0. In the main part of the program, we check the condition: if the number of milliseconds since the start of the microcontroller minus the number written to the timing variable is greater than 10000, then an action is performed to output a message to the port monitor and the current time value is written to the variable. As a result of the program's operation, the message 10 seconds will be displayed in the port monitor every 10 seconds. This method allows you to blink the LED without delay.

micros function instead of delay

This function can also delay without using the delay command. It works in exactly the same way as millis, but it counts not milliseconds, but microseconds with a resolution of 4 µs. Its maximum value is 4294967295 microseconds or 70 minutes. On overflow, the value is simply reset to 0, don't forget that.

Summary

The Arduino platform provides us with several ways to implement delay in our project. With delay, you can quickly pause the execution of the sketch, but at the same time block the operation of the microcontroller. Using the millis command allows you to get rid of the delay in Arduino, but this requires a little more programming. Choose the best method depending on the complexity of your project. As a rule, in simple sketches and with a delay of less than 10 seconds, delay is used. If the operation logic is more complicated and a large delay is required, then it is better to use millis instead of delay.