Final Year Django
Final Year Django
Experiment 2
2. Objective:
a) To learn the Arduino language (syntax and structure, variables and
keyword, Control Structure, Basic Functions etc)
b) Understanding Arduino commands with examples
3. Theory:
What is Arduino?
Arduino boards are able to read analog or digital input signals from different
sensors and turn it into an output such as activating a motor, turning LED
on/off, connect to the cloud and many other actions.
You can control your board functions by sending a set of instructions to the
microcontroller on the board via Arduino IDE (referred to as uploading
software).
Unlike most previous programmable circuit boards, Arduino does not need an
extra piece of hardware (called a programmer) in order to load a new code onto
the board. You can simply use a USB cable.
Additionally, the Arduino IDE uses a simplified version of C++, making it
easier to learn to program.
Get the latest version from the download page. You can choose between the Installer (.exe)
and the Zip packages. We suggest you use the first one that installs directly everything you
need to use the Arduino Software (IDE), including the drivers. With the Zip package you need
to install the drivers manually. The Zip file is also useful if you want to create a portable
installation.
When the download finishes, proceed with the installation and please allow the driver installation
process when you get a warning from the operating system.
Choose the installation directory (we suggest to keep the default one)
Setup( ) function
Loop( ) function
1. pinMode()
[Digital I/O]
Description
Configures the specified pin to behave either as an input or an output. See the Digital
Pins page for details on the functionality of the pins.
Syntax
pinMode(pin, mode)
Parameters
Returns
Nothing
2. digitalRead()
[Digital I/O]
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the Arduino pin number you want to read
Returns
HIGH or LOW
3. digitalWrite()
[Digital I/O]
Description
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set
to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for
LOW.
Syntax
digitalWrite(pin, value)
Parameters
pin: the Arduino pin number.
value: HIGH or LOW.
Returns
Nothing
Example Code
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
4. analogRead()
[Analog I/O]
Description
Reads the value from the specified analog pin. Arduino boards contain a multichannel,
10-bit analog to digital converter. This means that it will map
Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to
A6 on MKR boards, A0 to A7 on the Mini and Nano, A0 to A15 on the Mega).
Returns
The analog reading on the pin. Although it is limited to the resolution of the analog to
digital converter (0-1023 for 10 bits or 0-4095 for 12 bits). Data type: int.
5. analogWrite()
[Analog I/O]
Description
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying
brightness’s or drive a motor at various speeds. After a call to analogWrite(), the pin
will generate a steady rectangular wave of the specified duty cycle until the next call to
analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin
Syntax
analogWrite(pin, value)
Parameters
pin: the Arduino pin to write to. Allowed data types: int. value: the
duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
Returns
Nothing
Sets the output to the LED proportional to the value read from the
potentiometer.
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023,
analogWrite values from 0 to 255
}
The simulation of Arduino and its component using open source simulation software
TinkerCad-
Conclusion: Successfully learnt how to use the arduino IDE for compiling and
uploading the programs on the Arduino uno board and learnt the basic functions
needed to program using Arduino. Also we have studied the environment of open
source Simulation software Tinkercad.