Arduino Learning
Arduino Learning
Basic Electronics
breadboard
As you can see in the picture, There are holes extending. These holes are
in our feed channel. Descending the holes corresponding to the lines are
short-circuited. That is, a cable connected from red wires with cables to
connect over the same line It is associated. The holes corresponding to the
horizontal lines in the middle of the bear are linked to each other. If you
notice a slit from the middle of the horizontal lines pass. We can easily fit
our intentional inte It is that it provides
Resistance
Voltage Splitter
It is the inverter which reduces the input voltage to the desired voltage. Set
up like this. Exit The voltage depends on the resistances of R1 and
R2. Vout = Vg * R2 / (R1 + R2). for example If R1 = 10k R2 = 10k ohms
and the input voltage is 5 volts, then my output voltage = 5 * 10K / (10K +
10K) = 2.5 volts.
diodes
There are various diodes according to usage areas. Led, photocell, and
solar diodes
for example. As we know from our high school knowledge, the diodes
pass in one direction.
transistor
}
a == 5 here constitutes our condition. It should be noted that two equals
It is used. It means that it is equivalent. I can use it in the condition field
phrases,
==
Denk is
!=
If not equal
<
great is
>
small is
<=
Greater than or equal to
>=
Small or equal
Condition1 && Condition2 And
Condition1 | Condition2
Or
Our conditions are not obligatory to use else. Only iF can be used, so the
condition
If not, an extradan code will not work. New conditions with elseif if we
have more than one condition
We can add.
If (a == 1) {
// this works if a = 1
}
elseif (a == 2) {
// this works in case of a = 2
}
elseif (a == 3) {
// this works if a = 3
}
For loop: We may want the codes we write to repeat for a certain period
of time. For this
we must use cycles.
For (int i = 0; i <10; i ++) {
// this will be read 10 times
// the program will be incremented every time i get here
// loop will continue until i reaches 10
}
While loop: While loops like For are also used for looping.
b = 20;
while (b> 10) {
// Loop continues if b variable is greater than 10
b = b - 1; // reduce the value of b in each cycle by one
// be careful that if we did not change the value of b
// the loop condition will always be correct so the program will stay here
}
Tools menu: From here we use arduino Let's choose the COM port. If we
do not know this, we can look at it from the device manager. Program
compile: With this button we can check the program we have written. If
the error in the code if there is an error, we will make an orange line in the
black section below.
Reminder: The number of lines that we find is left-justified.
Program Compile & Upload: This button compiles the code we wrote
first. If the error in the code otherwise the code is translated into the
language Arduino understands and automatically dropped into Arduino.
During the process, you can see the progress of the process from the
progress bar. Full program rollout the Tx and Rx LEDs on the Arduino
will flash rapidly.
Once you have the program, you will have to hire Arduino.
Serial Monitor: This button opens a new window to send the data we
send from Arduino we can see. We will use this button frequently in our
future issues.
I wish you success in your Arduino work already ... So let's start.
Let me first learn the LED connection. When looking at the LED's feet
it seems that one of the long ones is short. We understand from here
What you need to do is to connect the long leg + to the short leg - on the
short leg should. So we will tie our long feet to arduino. But If there is no
connection between the feet, the short foot + tip, other foot - tip. Operating
current of our LED has. To do this and not to draw too much current with
the LED's
we must connect a 220 ohm resistor between the arduino (+ 5 volts
supply). Otherwise Our LED could explode. The resulting gas may be
harmful to health.
Now we can log in to the program. Arduino programs consist of two main
functions.
void setup () function: This function runs once when the program is first
opened, calibration, setup commands. void loop () function: the main ()
function we are used to from other programming languages like
this. Differently, the loop function starts from the beginning when the
function is finished,
it is actually a loop.
void setup () {
pinMode (13, OUTPUT );
pinMode (12, OUTPUT );
pinMode (11, OUTPUT );
pinMode (10, OUTPUT );
pinMode (9, OUTPUT );
}
void loop () {
digitalWrite (9, HIGH );
delay (10);
digitalWrite (9, LOW );
digitalWrite (10, HIGH );
delay (10);
digitalWrite (10, LOW );
digitalWrite (11, HIGH );
delay (10);
digitalWrite (11, LOW );
digitalWrite (12, HIGH );
delay (10);
digitalWrite (12, LOW );
digitalWrite (13, HIGH );
delay (10);
digitalWrite (13, LOW );
}
The codes given above for comprehension of the working principle of the
black lightning application
I think it is explanatory. But these codes are at the beginning level,
Each led is individually checked. As the next step,
Let's write a program that does it but is more professional.
const int ledPini [] = {9,10,11,12,13};
void setup () {
for ( int i = 0; i <5; i ++)
{
pinMode (ledPini [i], OUTPUT ); // We defined the LED pins as output
}
}
void loop () {
for ( int i = 0; i <5; i) {
digitalWrite (ledPini [i], HIGH );
delay (50);
digitalWrite (ledPini [i], LOW );
}
for ( int j = 4; j & gt; -1; j--)
{
digitalWrite (ledPini [j], HIGH );
delay (50);
digitalWrite (ledPini [j], LOW );
}
We will use buttons for various tasks in our applications. First of all,
Let's examine the logic. When the user clicks on the button, the button
becomes short-circuited so that the current begins to flow. When the user
takes the hand grip, pull-up / pull-down resistance system is used. With
these resistances used, the button rescued from being electrically
depressed.
Pull-Down Resistance
When the buton is pressed, the 5 Volt arduinon input reach the
foot. However,
5 volt voltage at arduinoun pin when we remove It remains. To get rid of
this
usually a resistance of 10K ohms arduinoun from the input foot to the
ground.
Pull-up Resistance
If you need to check the values you need to transfer the processed values
to the user we need to communicate between our army and our
computer. This communication RX and TX feet of the army will take
place. As always for communication It is enough if we connect my
computer via USB. Setup for communication let's not forget to write the
initialization code for our function. With the following code, second.
void setup () {
Serial . begin (9600); // communication between computer and arduino
We are launching.
// We wrote 9600 to run your computer and the army at the same speed.
// So at the moment there will be 9600 bit transfers.
}
int counter = 0;
void loop () {
Serial . print ("Counter Digit ="); // We are printing our message on the
screen.
Serial . println (counter); // print our counter value on screen and add new
line
We are going through.
delay (1000);
// Wait a second
}
The whole process is that easy. It's up to you now. What you write with
this link
you can easily test and debug the program.
Now we know that the button we have previously learned is in control,
Let's make an incremental counter when pressed.
const int buttonPin = 2; // Button pin number
int counter = 0; // Our counter
int buttonState = 0; // Button state
void setup () {
pinMode (buttonPin, INPUT );
Serial . begin (9600);
}
void loop () {
buttonState = digitalRead (buttonPin);
if (buttonState == HIGH ) {
counter ++; // counter = counter + 1;
Serial . print ( "Counter Digit =" ); // Display message
We are printing.
Serial . println (counter); // print our counter value on screen and add new
line
We are going through.
while (buttonState == HIGH ) { // Butondan hand pick up program
it stays here
// Thus, each time the button is pressed, the value is incremented only once
buttonState = digitalRead (buttonPin);
}
}
}
I will always send data to arduino, let's expect some data from the other
side. With this code,
the incoming data are processed in arduin. Do not forget that all the data is
It comes.
int incomingByte = 0; // incoming data
void setup () {
Serial . begin (9600);
}
void loop () {
if ( Serial . available ()> 0) { // expect data from the computer
incomingByte = Serial . read (); // read incoming data
Serial . print ( "incoming data:" );
Serial . println (incomingByte, DEC ); // return the incoming data to the
computer
send
}
}
The part on the arduino is not so difficult. But our bluetooth module
we need to match. After you have connected your Bluetooth tunnels
the red light will flash. This means that the light is waiting for the
connection.
Let's find our module by searching our computer's bluetooth. Match the
devices.
If you ask for a password, usually the first conviction is 1234. If we are
properly connected the red light will always be on. More detailed
information on this part is available from the link below You can reach. A
new COM Port will be opened for the module, we will use it in our
programs.
http://www.mcu-turkey.com/stm8s-hc-06-bluetooth-modul-ile-
haberlesme-uygulamasi/
Let's write the code now. In fact, we will not do anything very
different. Serial communication we can download the sample code to my
arduinumuza.
void setup () {
Serial . begin (9600);
}
int counter = 0;
void loop () {
Serial . print ( "Counter Digit =" );
Serial . println (counter);
delay (1000);
}
We programmed our program and carefully touched the Tx and Rx feet of
the bluetooth module
We connected the arduino. The red light in the module is still
flashing. Because the computer module (Master -> Slave connection)
In this step, we need an additional program. This program allows the COM
ports
listening program. There is no need to have a very special program.
There are programs. My suggestion is Tera Term. You can download from
the link below.
http://download.cnet.com/tera-term/3000-20432_4-75766675.html
Let's run the program and connect it modularly. In the connection settings,
Let's choose the COM port we introduced to the computer. If everything is
true now the red light is continuous burning and data exchange occurs. If
there is no data flow control our settings
Let me choose the other bluetooth COM port and try again. I hope the data
flow
Provide Abilmişsinizdir. All steps must be done carefully.
Now you can ask 'Do we have to deal with Tx Rx cables every time?' Our
answer is NO.
We can define new communication legs in your army. Thus, by computer
communication a conflict does not occur.
With the following code, we will connect our module now with feet 10 and
11.
Everything in the real world is analog. But this is not possible in the digital
world. Analogue digital to store and process data. More theoretical
I do not plan to enter but how I have to share the picture on the side for
you to understand. As I think it is an attempt from 0 to 5 volts
there is. To introduce the values of this input to Arduino, use the ADC
(Analog Digital Convert) We'll use. We will give 0 Volta 0 and we will
give 1023 in 5 Volta. So 10 bits we will use a resolution ADC. Oran
continues as a liner. So,
A change of 0.004 volts will correspond to 1 digit digitally. That is, 0.44
Volt => 100. Arduino makes this transformation for us himself. But the
cables we will measure We need to link to the analog legs of the
Arduino. The analog value in the enterprise is digital
CNY70 How it works: emitting infrared light on and there are two LEDs
that can recollect this light. The beam from the infrared LED strikes the
surface and
the collector LED. The LED measures the intensity of this light. If the
surface is black or white, the reflected light it changes its severity. This is
what we need
black and white separation. Our sensor analog.
That is to say, the analog-to-analogue reading function
We'll use. First of all, we need to set up a vacation. For this reason, we
recommend perilaks My rule. When placing the sensor, be careful that the
feet are shaped like this.
As an example, let's start with a connection of one sensor at first and a
project with one sensor I'm of carrying. 3 lines combine our advanced
projects following a simple one sensor The robot will do.
No links in our rules breadboard. Beware of false connections
Binding. The tile-shaped diamond sensor would be put on the wiring
breadboar
Easier.
Let's start writing code that we perform the installation as in the picture.
int referansdeger = 800; // threshold for black and white
void setup () {
Serial . begin (9600);
}
void loop () {
int sensordeger U = analogread (A1); // Connect to A1 foot Arduino
tension on the cable is measured
Serial . print (sensordeger a); // value is printed on the screen we read
if (sensordeger of> referansdeger a) {
Serial . println ( "Black" );
}
else {
Serial . println ( "White" );
}
delay (1); // we give a short wait to work properly.
}
referansdeger in the program constitutes our threshold. The value of your
sensor, you
You will need to adjust accordingly. You can run the program examined
the value you get. in black You divide the value by picking up the value in
white. This new value is yours you referansdeger'i
It will create.
If you believe that your sensor is working control for Android-based phone
Keep toward the camera sensor (not work on some android and iPhone). If
the light sensor If you see your feed illuminates the connection is
correct. You still will not operate analog sensors Check the sensor
connection with his foot once more.
I do not use often, even with the Arduino would be useful to learn to use
the LCD. Always Find the computer may not be able to show our data. In
such cases using lcd'miz You can get print status screen. Cable
connections can be a bit complicated and first
LCD may not use your experiment and make mistakes properly. That's
why
Make your careful circuit connection.
If we've carefully Circuit can now no longer our programming. All
characters
previously defined for us. Our library is primarily to reach these characters
We will include in our program. Then we made our initial settings and the
lcd'miz
We'll start using it.
Come on let's write something to our screen.
#include < liquidcrystal .h> // we add our library
Liquidcrystal LCD (12, 11, 5, 4, 3, 2); Cable connection to our program //
We introduced
void setup () {
LCDs. The burner (16, 2); // lcdmiz we enter the number of columns and
rows
LCDs. print ( "Hasbi Joy" ); // Display to something I've written
}
void loop () {
LCDs. setCursor is (0, 1); // 2. We have reduced our cursor line
LCDs. print ( Millis () / 1000); The time elapsed since the beginning of the
program //
}
We found our results and variable values even if the power is interrupted
in the Arduino We may want to save. built-in EEPROM for the Arduino
that we can provide There. Variable values that can hold about our
program EEPROM, electrical It is written as a small removable storage. on
the Arduino microcontroller It varies according to the type EEPROM
capacity. 1024 bytes of ATmega328, and ATmega168 atmega8 512 bytes,
whereas the ATmega1280 and atmega2560 has 4 KB of storage.
This storage space is sufficient for storing the values required for our
program.
Let's Let's write a sample program
We begin our slow arduino to control the outside world. servo motor with
an Arduino rides compared to other microcontrollers (especially based on
PICs) is easier. weeks with PIC It takes time minutes with the servo motor
control arduino.
#include < Servo .h> // We have included the program our servo library
Servo servonesne of; // we create an object for servo control
void setup ()
{
The servonesne. attach (9); // 9 pin cable to power our orange
We say we wear
}
void loop ()
{
The servonesne. write (100);
// Servo our 100 degree
We return
delay (1000);
// Let's wait a bit
The servonesne. write (20);
// we rotate 20 degrees to power our
delay (1000);
// Let's wait a bit
}
http://www.aliexpress.com/item/special-promotions-5pcs-lot-l298n-motor-
driver-board-module-L298-for-arduino-stepper-motor-smart-car /
1827893830.html
I'll tell you soon can you get this card and cable connections as your
engine
You can easily check.
But if you want to make this business a bit challenging yourself and the
entire circuit can be complex.
But to understand how the system works at least once in the establishment
of circuit useful can. To control for motor drive and motor to strengthen
the current L298 We will use the integrated. Similar integrated and serves
the same task. most of integrated L298 important features is able to
withstand up to 2 amps and the presence of two H-bridge.
H Bridge:
H bridge and the engine of our future driving in the reverse direction
providing structure. 4 consists of transistors .
Our integrated There are a total of 15 feet. Some of these engines to our
feet
Some will be connected to the Arduino and some feed. Integrated pin for
structuring You can use pictures.
INPUT 1 and INPUT 2 (5 and 7 feet): We'll connect our Arduino these
pins. Input 1 to 5 When we give our engine-volt input 2 to 0 volts forward,
when we do the opposite Will go backwards.
INPUT INPUT 3 and 4 (10 and 12 feet): These pins also works as
INPUT 1 and INPUT 2.
As the name suggests, this pin 5 volts It should be connected. between the
soil to reduce the instability of the circuit, this pin 100nf'lık connected
capacitor.
GND (8 foot) are required to connect these pins to ground. Also integrated
on topmetal is the soil. Why mention that you do not accidentally shorted
my
It is for.
VS (4 feet): Integrated feed our feet is the main engine provides the
energy to go on. Here we give our motor control will be in line with the
engine. our engine
12 Volt here we connect our properties considering the application.
Let's start building our integrated circuit As we learned from our pin
structured. Test You can use to integrate on a breadboard. However, use in
other applications If you want, and constantly crowded the cable / cable
due to lack of contact makes full Do not yield. After testing circuit so
perforated or printing pertinaks You may want to play. I'm ready to take
my advice or circuit board. from abroad You can get 2-3 $ 'a.
When you get ready, you will see card or via pins I mentioned just now.
Rank came to writing the Arduino software. The following code in other
functions formed, We will also use our application. First INPUT variables
for our feet We define.
const int milking of = 9;
const int sagger 8 =;
const int soli in = 12;
const int Solger = 13;
const int solenabl A = 11;
const int sagenabl = 10;
void forward ( int hizsag, int hizsol) {
analogwrit A (sagenabl A, hizsag);
digitalwrit A (milking of, HIGH );
digitalwrit A (sagger is LOW );
analogwrit A (solenabl A, hizsol);
digitalwrit A (soli are HIGH );
digitalwrit A (Solger is LOW );
}
void reverse ( int hizsag, int hizsol) { // the second right motor, our first
variable
It shows the speed of the left motor.
// our engine speed should be between 0-255.
However, some of the engine torque by deficiencies // 60-
255 works.
// If you need to increase the speed of your engine coming off a high-
pitched voice
Stop(); // we stopped
delay (1000);
forward (100.150) // we are going the right light
delay (1000);
Stop(); // we stopped
delay (1000);
return (100,100); // We're going back
delay (1000);
Stop(); // we stopped
delay (1000);
}
I hope has been useful codes. Now, a code that we wrote with the Arduino
software The tables show a connection between the motor drive our feet
with his feet.
Arduino
Motorcycle driver
8
INPUT 1
9
INPUT 2
13
INPUT 3
12
INPUT 4
11th
ENABLE A
10
ENABLE B
Engine
Motorcycle driver
motor1 +
OUTPUT 1
motor1 -
OUTPUT 2
Motor2 +
OUTPUT 3
Motor2 -
OUTPUT 4
(Motor + or - does not matter which one end of)
Feed
Motorcycle driver
+12 Volts
VCC
Ground (- end)
GND
+5 Volt
ETC
In our book we do not practice with the HC-SR04 Distance Sensor. This
sensor market in abundance which it is expensive, but a sensor located in
our country. Out of the $ 1-2 China 'You get a. It measures the distance
between the sensor 2-200 CM. But 200 cm right sensor quality It is
impaired.
Roads can hear sound at a frequency of the sensor human ear. Sound if hit
somewhere back and our revenue neutral sensor. The sensor calculates the
commute time so that voice and body
Find the distance. So this sensor is also called ultrasonic sound sensor.
You can look at the image below for the circuit diagram.
wiring diagram as shown in the picture
Adxl345 we will use the most widely used in the market for acceleration
measurement. 3 axes of this sensor angular acceleration can be
measured. Sensor I 2 C and SPI communicates through line. Come on
link our rules:
As we can now connect to our programming circuits. Before using our
Sensor
Let's get ready for the ADXL345 where the library functions. You can
download the form.
http://code.bildr.org/download/959.zip
Library files into the program the Arduino libraries files between folders
Let's take. According to examine the sample code library complete the
installation.
Arduino APPLICATIONS
Traffic lights
Objective: The red light will illuminate for 5 seconds. Later After half a
second yellow light and a green light for 3 seconds Lights. But a second,
and then the yellow light turns red
Light is passed.
Our goal is to better understand ADC. We will only use two of our cable
connection.
First let's connect the ground wire to the Arduino our feet. A0 our feet on
the second cable Let's tie. What you can establish test circuit to test the
voltmeter and its various You can measure the voltage in place.
Now let's software. Unlike this time corresponding to the numerical value
of our ADC sample We will print the voltage is calculated on the computer
screen.
void setup () {
Serial . begin (9600);
}
void loop () {
int sensordeger U = analogread (A0); // numerical value of the voltage at
the A0
float voltage = (5/1023) * sensordeger of; // 5 volt max. by 1023
It was measured. We do 5/1023 So step process to find the range.
We multiply the value of this result, we have found so ADC voltage.
Serial . print (voltage);
Serial . println ( "V" );
delay (100);
}
Where you want to measure the voltage measurement cable from the A0,
in the circuit of your soil Connect the ends from GND.
Let's make connections as illustrated. When you look at the location of the
sensor core in a row, so write us see when the first pin 5 volts, the middle
pin Arduino analog input pin 3 and the ground (GND) It must be
connected.
Come on, let's move on software ... read analog signals and we need to do
with mathematical operations to translate the temperature. Translate value
that we'll send to the computer.
float temperature;
void setup ()
{
Serial . begin (9600);
}
void loop ()
{
Temperature = analogread (A0);
Temperatures * Temperature = 0.48828125;
Serial . print ( "TEMPERATURE =" );
Serial . print (heat);
Serial . println ( "C" );
delay (500);
Let's start making our first robot. we see from the course to make our robot
We will use our sensor on our and DC motor drives. If have information
about these topics Do not be useful to review these issues again.
Come on, Let's get started ... First, let's get our materials list:
- 1 chassis: a plate to secure all materials and engines
- 2 DC motors
- 2 wheels: DC motor end must fully comply
- 1 motor drive: is it made our own
- 1 wheel drunk: Or a rod to reduce friction with the surface (I usually
I use the LED head)
- Battery: 11.1 Volt LiPo batteries would be appropriate. 9-volt battery is
inadequate for engines
Try going to need
- 3 x HC-SR04 Distance Sensor
- And of course, an Arduino
Let's first robot mechanics. our chassis Let's place behind the two
engines. The front of the robot Let's put our drunk one. If we Drunk LEDs
underneath the robot so as to ensure a balance Let's put. and our DC
motor's put our wheels
Let 's stand in the balance of our robot. Now a nice place for our Arduino
and motor drives Let's set. Do good robot comfortable wiring test It will
allow you to. Finally sensors 0-90-180 Let's place as degrees. placement of
the robot will be like in the picture. Electronic connections in previous
issues Let's carefully as we speak.
Let's go to the software that we've finished the wiring, and let us test our
robot.
Creatng some changes to the code and the code of robots to work in
accordance with You may need to make.
This can be done by robots robot that solves the maze. Here, it is exactly
90 degrees robot That he can return.
Let's design a robot similar to a previous project we have. But, this time
from sensors sokelee and facing the front of the robot and the floor to be 1-
2 cm above the ground, black / white Let's put our sensors. This robot will
use our CNY70 3 units. That's why 3 to tie one of these sensors in the way
we told before. First I must say that we have seen in a line following robot
competition quality up We will not do. That way we need to use the PID to
make a robot. But we need it We will do more than a simple robot without.
Come on, Let's get started ... First, let's get our materials list:
- 1 chassis: a plate to secure all materials and engines
- 2 DC motors
- 2 wheels: DC motor end must fully comply
- 1 motor drive: is it made our own
- 1 wheel drunk: Or a rod to reduce friction with the surface (I usually
I use the LED head)
- Battery: 11.1 Volt LiPo batteries would be appropriate. 9-volt battery is
inadequate for engines Try going to need
- 3 CNY70: Connections can use on a card making
- And of course, an Arduino
Let's start with the programming. In previous issues we have learned
through our project codes
We will write.
First, make sure you understand the issues that we do Arduino and
Bluetooth control. It We will manage our TERA TERM via bluetooth
using the robot program in the project. One Robot rules we set in our
previous projects. Our sensors and bluetooth draw Let's connect our
module.
- 1 chassis: a plate to secure all materials and engines
- 2 DC motors
- 2 wheels: DC motor end must fully comply
- 1 motor drive: is it made our own
- 1 wheel drunk: Or a rod to reduce friction with the surface (I usually
I use the LED head)
- Battery: 11.1 Volt LiPo batteries would be appropriate. 9-volt battery is
inadequate for engines Try going to need
- Bluetooth module
- And of course, an Arduino
If the Bluetooth module will be blinking light if we do everything
right. Speaking of more I want to get programming from the
extension. The robot is to act It will wait for commands from the
computer. This command keyboard out of our program TERA TERM
We'll give. The robot is the 'w' advanced, 'x' will go back; 'A' left, 'd' to
turn right and 's' key will stop by.
So let's start…