Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Arduino Programing Note

The document provides an overview of Arduino, including its IDE, microcontroller functions, and basic coding structure using C++. It explains the roles of various components such as analog and digital pins, as well as functions like pinMode(), digitalWrite(), and delay(). Additionally, it includes a simple example of controlling an LED with specified timing using Arduino code.

Uploaded by

solomonarid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arduino Programing Note

The document provides an overview of Arduino, including its IDE, microcontroller functions, and basic coding structure using C++. It explains the roles of various components such as analog and digital pins, as well as functions like pinMode(), digitalWrite(), and delay(). Additionally, it includes a simple example of controlling an LED with specified timing using Arduino code.

Uploaded by

solomonarid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Embedded Control System

Arduino coding
Level 5
What is Arduino
• Arduino IDE (Integrated Development
Environment) is an essential which makes the
task of uploading code on Arduino boards, an
easy task. Instead of writing them at the
assembly level, the IDEs make it convenient
and the codes are written in high-level
languages like C and C++.
• Arduino code is based on C++, a high-level
programming language
What is Arduino?

• Microcontroller: The microcontroller used on the Arduino board is essentially used for
controlling all major operations. The microcontroller is used to coordinate the input taken and
execute the code written in a high-level language.
• Analog Reference pin: Analog pins are used for general purposes like supporting 10-bit analog-
to-digital conversion (ADC) which is performed using analog the Read() function. Analog pins are
particularly helpful since they can store 0-255 bits which is not possible using digital pins.
• Digital Pins: Digital pins are used for general purposes like taking input or generating output.
The commands that are used for setting the modes of the pins are pinMode(), digitalRead(), and
digitalWrite() commands.
• Reset Button: The reset button on the Arduino board is used for setting all the components of
Arduino to their default values. In case you want to stop the Arduino in between you can use
this reset button.
• Power and Ground Pins: As the name suggests, power and ground pins are used to supply the
power needed for driving the Arduino board. The ground pins are usually 0V to set a reference
level for the circuit.
• USB (universal serial bus): The Arduino needs certain protocols for communication purposes
and the universal serial bus is used for this purpose. It helps to connect Arduino,
microcontrollers with other raspberry pies.
Basic Operation

• Most Arduino boards are designed to have a single program


running on the microcontroller.
• This program can be designed to perform one single action, such
as blinking an LED.
• It can also be designed to execute hundreds of actions in a cycle.
The scope varies from one program to another.
• The program that is loaded to the microcontroller will start
execution as soon as it is powered. Every program has a function
called "loop". Inside the loop function, you can for example:
– Read a sensor.
– Turn on a light.
– Check whether a condition is met.
– All of the above.
Electronic Signals

1. Analog Signal: Analog signals can take any


value in a given continuous range of values.
Generally, analog signals used in Arduino are
around 0V to 5V. The analog pins can take
data up to 8-bit resolution therefore, they are
used for taking large values as input in the
Arduino. These signals carry data in a very
accurate form without many errors.
Electronic Signals

• Digital Signal: Digital signals can only take discrete


values which are, high('1') and low('0'). These
signals are usually used to Arduino on or off which
requires only two values. The collection of two
values (0 and 1) can be used to generate a
sequence known as the binary sequence which is a
collection of zeroes and ones. This is how data is
transmitted without much memory requirement
but this can lead to certain errors like quantization
errors.
Electronic Signals
Analog Signal Digital Signal
• Digital signals can only take discrete
• Analog signals can take any value values which are, high('1') and
in a given continuous range of low('0').
values. • These signals are usually used to
• Generally, analog signals used in Arduino on or off which requires only
two values.
Arduino are around 0V to 5V.
• The collection of two values (0 and 1)
• The analog pins can take data up can be used to generate a sequence
to 8-bit resolution therefore, known as the binary sequence which
they are used for taking large is a collection of zeroes and ones.
values as input in the Arduino. • This is how data is transmitted
• These signals carry data in a very without much memory requirement
accurate form without many but this can lead to certain errors like
errors. quantization errors.
Brackets
• There are two types of brackets utilized in Arduino coding, as
given below:
• Parentheses: When writing a function in IDE, the parentheses
brackets are used to include the argument parameters, such as
methods, functions, or code statements. In addition to this, the
bracket is also used for defining the precedence order while
dealing with mathematical equations. These brackets are
represented by '( )'.
• Curly Brackets: Curly brackets are used to open and close all the
statements in the functions or out of the functions. Note that a
closed curly bracket always follows the open curly bracket in the
code for proper layout. These brackets are denoted by '{ }'.
– Open curly bracket- ' { '
– Closed curly bracket - ' } '
Line Comment
• There are two types of line comments, let us study them individually:
• Single-line comment: As the name suggests, the single lines that follow
two forward slashes are known as single-line comments. These
statements are known as comments because the compiler ignores all the
characters that come after two forward slashes in a single line.
Comments are hidden when the output is presented. Comments are
added for the sole purpose of comprehension of the code and for writing
necessary information for user reference.
// This is a comment
• Multi-line comment: The single line comment extends to one line and
the Multi-line comment is used for adding comments in multiple lines.
The syntax is a forward slash followed by an asterisk symbol (/*), ending
with a */. It is mostly used for commenting larger text blocks that are not
interpreted by the compiler and solely for reference purposes of users.
/ * This is a multiline comment*/
Coding Screen

• If you open the coding screen of your IDE, you


will realize that it is divided into two sections
namely, setup() and loop(). The setup
segment is the first block and is implemented
first for preparing the necessary environment
needed for running other commands. This
coding screen is shown below:
Coding
• It is important to note that the setup and loop
blocks must have statements that are
enclosed within curly brackets. Depending on
the type of project you are working on, you
can initialize the setup in setup() and define
other necessary statements in the loop()
block. Let us study each section individually
Coding
• It is important to note that the For example
setup and loop blocks must void setup ( ) {
have statements that are Coding statement 1;
enclosed within curly brackets. Coding statement 2;
• Depending on the type of Coding statement n;
}
project you are working on, you
void loop ( ) {
can initialize the setup in setup()
Coding statement 1;
and define other necessary
Coding statement 2;
statements in the loop() block.
Coding statement n;
• Let us study each section }
individually
Coding
• Setup contains the very beginning section of the code that must be executed
first. The pin modes, libraries, variables, etc., are included in the setup section
so that no problem occurs when the remaining code runs. It is executed only
once during the uploading of the program and after resetting or powering up
the Arduino board.
• Zero setup () resides at the top of each sketch. When the program runs after
completion, it heads towards the setup section to initialize the setup and
include all the necessary libraries all at once.
• Loop The loop contains statements that are executed repeatedly. Unlike, the
setup section there is no restriction on running this code once, it can run
multiple times according to the value of variables.
• Time The basic unit of measuring time in Arduino programming is a
millisecond.
1 sec = 1000 milliseconds
• Timing adjustments can be made in milliseconds. A better explanation for this
can be that a 2-second delay corresponds to 2000 milliseconds.
Simple example

• A of blinking the LED using Arduino is


considered.
The steps are:
• Go to the menu bar. Click on the File button in
the bar.
• Click on the Examples in the menu bar.
• Click on the Basics option.
• You will see Blink, click it.
• Note: The pinMode will be the main function
in the void setup() and digitalWrite( ) and
delay ( ) will be the main function in the void
setup()
PinMode ( )

• The pinMode() function assigns a specific PIN as either


INPUT or OUTPUT.
The Syntax is:
pinMode (pin, mode)
• Pin: It is used to specify the PIN which depends on the
project requirements.
• Mode: Depending on whether the pin is taking INPUT or
OUTPUT, it specifies the pin's function.
• Let's consider a situation to understand the pinMode. We
want to take input from the PIN 13 and then,
• Code:
pinMode (13, INPUT);
PinMode in OUTPUT mode

• Setting pinMode to OUTPUT is important for


some pins. This mode allows the specified PIN to
supply sufficient current to another circuit to
activate the sensor or light the LED. When set to
OUTPUT, this pin goes into a very low impedance
state, making the current useful. It is important to
note that excessive current or short circuits
between pins can damage the Atmel chip. This
explains the need for setting the mode to
OUTPUT.
PinMode in INPUT mode

• When digitalWrite() is used, selecting the INPUT


mode for any pin turns off the low state and sets the
high state as the ultimate state. The INPUT mode
can be employed alongside an external pull-down
resistor. For this purpose, pinMode should be set to
INPUT_PULLUP. This configuration reverses the
behavior of the INPUT mode. In INPUT_PULLUP
mode, a sufficient current is provided to light an LED
connected to the pin dimly. If the LED emits a dim
light, it signifies that this condition is operational.
digitalWrite( )
• The digitalWrite( ) function is used to decide the value
of the pin. It can be set as either of the two values, HIGH
or LOW.
• HIGH: For a board that is supplied with a maximum of
1V, it results in a 5V value whereas on a board with
other values like 6V, it updates the value to 6V.
• LOW: It sets the pin to the ground by setting a reference
of 0V.
• If no pin is set with pinMode as OUTPUT, the LED may
light dim.
• The syntax is:
digitalWrite( pin, value HIGH/LOW)
digitalWrite( )

• Pin: We can specify the PIN or the declared variable.


Let's understand with an example.
Example:
digitalWrite (6, HIGH);
digitalWrite (6, LOW);
• The HIGH will be used for setting the pin at number
6 high and it will ultimately turn on the LED if
connected to this pin while, the LOW will be used
for setting the pin at number 6 low and it will
ultimately turn off the LED if connected to this pin.
delay ( )
• The delay() function • Code:
digitalWrite (12, HIGH);
serves as a tool to halt
delay (5000);
program execution for a
digitalWrite (12, LOW);
specified duration, delay (2000);
measured in milliseconds. • The program here is that the LED is
We have seen how connected to the pin having PIN 12
delay(5000) signifies a and it will remain lit for 5 seconds
before turning and then will go off.
stop of 5 seconds. • The LED will then be turned off for
• This can be understood 2 seconds as specified by delay().
by the fact that 1 second This cycle will continue in a loop
depending on the defined variables
equals 1000 milliseconds. within the void loop() function.
Solved Example

• Let us try to code the control of the LED on


PIN 12, by designing it to remain ON for 3
seconds and remain OFF for 2.5 seconds.
Here is the code
EXAMPLE
• Firstly, we will need to set a particular pin as the output
pin therefore, we will set the pin number 12 as the input in
setup() block.
• Then we need to set the pin number 12 high using the
digitalWrite() function.
• Then we use the delay() function to keep the LED on for 3
seconds.
• Then we need to set the pin number 12 low using the
digitalWrite() function.
• Then we use the delay() function to keep the LED off for
2.5 seconds.
EXAMPLE
void setup ()
{
pinMode ( 12, OUTPUT); // to set the OUTPUT mode of pin number 12

}
void loop () {
digitalWrite (12, HIGH);
delay (3000); // 3 seconds = 3 x 1000 milliseconds

digitalWrite (12, LOW);


delay (2500); // 2.5 seconds = 2.5 x 1000 milliseconds

You might also like