Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Arduino IDE (Integrated Development Environment)

- An Arduino program is called as skecth.


- Free programming environment developed by Arduino
- Programs the microcontroller
Function buttons

Tab: Your skecth can


be written in multiple
files. You can switch
in between.

Editor area which you


write the code

Messages area: Indicates the process,


errors and warnings.
Verify button
Serial monitor:
Conveys text
messages from
Arduino to your
computer

We can print the


information coming
from a sensor.
Shows all the compilation
errors and processing
information
Line
numbers

Folding enabled
Modifies your code into a proper order

When you download a sketch from a website,


corrects and fixes the typos (writing mistakes)
Start a serial connection: Sends
texts back to Arduino IDE
Serial monitor
Serial plotter
Arduino IDE Programming Basics
- C++ programming language
- The code is compiled to Arduino microcontroller
- setup() : inside the parentheses is empty, which means no input
parameter is required

- Serial.begin() requires a single parameter, the baud rate (bits per


second) of communication. Valid rates are 300, 600, 1200, 2400,
4800, 9600, 14400, 19200, 28800, 38400, 57600 and 115200.

- For most purposes, 9600 is a good value that makes communication


fast enough without stressing your PC or the Arduino.
Name of class
Name of
particular
function

Serial.println: Print something and create a new line


Serial.print: Print something with no new line
Set the baud rate as the
same for the code and
the serial monitor to
work properly.
millis(): give the number of miliseconds since the sketch began executing on the Arduino
delay(1000): stop for 1000 miliseconds

If you press Reset button on Arduino card, it starts running the code from the beginning
Check Upload
Custom functions

Function: A group of instructions that you put together with a name

void loop() int do_calc()


{
Serial.println(1+1);
Return Name
}
Set of
type
parameters

do_a_calc(5,6)

int do_a_calc(int number_1, int number_2)


{
Serial.println(number_1 + number_2);
}
int do_a_calc(int number_1, int number_2)
{
Serial.println(number_1 + number_2);
}

void do_a_calc(int number_1, int number_2)


{
Serial.println(number_1 + number_2);
}
int do_a_calc(int number_1, int number_2)
{
// Serial.println(number_1 + number_2); (Commented line)
return number_1 + number_2;
}

return command needs to placed at the end of the function.


int do_a_calc(int number_1, int number_2)
{
return number_1 + number_2;
Serial.println(number_1 + number_2);
}

Will not be executed since placed after the return command


Conflict
The Arduino UNO only has 2KBytes
of RAM, which is the part of the
memory where variables are stored.
Returning to the assigned value
The variable ‘first_number’ is not
defined in the scope of the
‘do_a_calc’ function, and therefore
the code resulted an error.
If a variable is defined at the root of the code,
then all the scopes will recognize the
variable.

root: The lines at the top of the ‘void setup()’


Constants

const int first_number=5;

Conflict
Conditionals (if statement)

counter=counter+1;
Conditionals (while statement)
Conditionals (while statement)
Control structures (for loop)
Control structures (switch) The ‘default’ keyword is optional. If you want
to use it, place it after the last case block.

If you don’t use it, only the blocks that


explicitely match the switch variable will be
executed.
Digital/Analog pins
- Digital pins are useful for reading the state of devices like buttons,
switches, transistors or LEDs.
- These devices have one thing in common: They have two possible
states. (e.g. a button can either be pressed or not pressed)

LEDs
- Long side of LED is called Anode (+), short side is Cathode (-)
1000 ohm resistor to
protect LED
Set digital pin8 as output

Give 5V to digital pin8

Give 0V to digital pin8


When the button is not pressed, the
pin of the button that is connected to
digital pin does not have a particular
state (neither on nor off). Very large
resistors (10000 Ohms) are used and
connected to ground.

When the button is not pressed,


yellow wire will be grounded. When
button is pressed, yellow wire has 5V
through the red wire.
Pulse width modulation (PWM):
- It is a technique by which the effect of an analog signal is simulated
using a digital (square wave) signal.
V V

5 5

t t

Potentiometer: Variable resistor


In Arduino Uno, the analog
reading resolution is 10-bits,
which provides space for 1024
voltage levels.
Potentiometer

From the analog pin, you will


read a value between 0-1023.
analogRead provides values within 0 to 1023 due to analog pin.
Since, digital pin 7 is not a PWM pin, it can not generate varying signal.
Therefore, LED will be completely ON or completely OFF.
Mapping provides to change the analogWrite returns values
function according to the bounding between 0 and 255.
minimum and maximum values.
Select a PWM pin to have varying brightness on the LED.
Change digital pin 7 to digital pin 6 with PWM.
The digital pins with tilda (~) are PWM pins.
‘#define’ is similar to ‘const int’
RED_PIN is set to 3, no need to use ‘ = ’ and ‘; ’

You might also like