Unit 3
Unit 3
Unit 3
Satya P. Singh
Electronics Design Workshop
3.1 Introduction
Satya P. Singh
Electronics Design Workshop
Satya P. Singh
Electronics Design Workshop
3.3 Getting acquainted with the Arduino IDE and Basic Sketch
structure
1 void setup() {
2 pinMode(13, OUTPUT); // configure pin 13 as an output
3 }
4
5 void loop() {
6 digitalWrite(13, HIGH); // turn on the LED
7 delay(1000); // wait for 1 second
8 digitalWrite(13, LOW); // turn off the LED
9 delay(1000); // wait for 1 second
10 }
This sketch configures the board's pin 13 as an output in the setup() function, and then
repeatedly turns the LED on and off with a 1-second delay in the loop() function.
Arduino programming also allows you to use a variety of libraries that provide pre-
written code for common tasks such as reading sensors, controlling motors, and
communicating over networks. These libraries can save a lot of time and make it easy
to add advanced functionality to your projects.
Digital Input and Output (I/O) in Arduino refers to the process of reading or writing a
digital signal, which can only have two states: HIGH or LOW (1 or 0). In Arduino, digital
I/O is accomplished using digital pins on the board.
Digital Output: To write a digital value to a pin, you can use the digitalWrite()
function. This function takes two parameters: the pin number and the value you want
to write (HIGH or LOW). For example, to set pin 13 to HIGH:
1 digitalWrite(13, HIGH);
Digital Input: To read a digital value from a pin, you can use the digitalRead()
function. This function takes one parameter: the pin number. It returns the value of the
pin (HIGH or LOW). For example, to read the value of pin 2:
1 int buttonState = digitalRead(2);
Satya P. Singh
Electronics Design Workshop
It is important to note that before using a pin as input, you must set its mode using
the pinMode() function. For example, to set pin 2 as an input:
1 pinMode(2, INPUT);
Additionally, you can also use the pull-up or pull-down resistors to set the default state
of the input pin, when it is not connected to anything. For example, to enable the
internal pull-up resistor on pin 2:
1 pinMode(2, INPUT_PULLUP);
In summary, to use a pin as a digital input or output in Arduino, you should set the pin
mode using pinMode(), read the digital value using digitalRead() or write the digital
value using digitalWrite().
In Arduino, you can measure time and events using a variety of built-in functions and
libraries. Here are a few common examples:
1. delay(): This function causes the program to pause for a specified number of
milliseconds. For example, the following code will turn on an LED for one
second:
1 digitalWrite(13, HIGH);
2 delay(1000);
3 digitalWrite(13, LOW);
2. millis(): This function returns the number of milliseconds that have passed
since the Arduino board began running the current sketch. This can be useful
for measuring how much time has passed since a certain event occurred. For
example, the following code will blink an LED once per second:
Satya P. Singh
Electronics Design Workshop
example, you can use the Timer library to create a timer that calls a function
after a specified interval.
5. Interrupts: Arduino boards also support external interrupts, which allow you to
detect specific events and respond to them immediately, rather than waiting for
the loop() function to check for them. For example, you can use the
attachInterrupt() function to call a function when a button is pressed.
In summary, measuring time and events using Arduino can be done using built-in
functions like delay(), millis() and micros(). You can also use libraries and timer/counter
or interrupts to achieve this.
In addition to the analogWrite() function, you can use the tone() function to generate
PWM signals to drive speakers or other devices.
Here is an example of Arduino code that uses PWM to control the brightness of an
LED connected to pin 9:
1 void setup() {
2 // Set pin 9 as an output
3 pinMode(9, OUTPUT);
4 }
5
6 void loop() {
7 // Gradually increase the brightness of the LED
8 for (int i = 0; i < 255; i++) {
9 analogWrite(9, i);
10 delay(10);
11 }
12
13 // Gradually decrease the brightness of the LED
14 for (int i = 255; i > 0; i--) {
15 analogWrite(9, i);
16 delay(10);
17 }
18 }
Satya P. Singh
Electronics Design Workshop
1 void setup() {
2 // Set the baud rate for serial communication
3 Serial.begin(9600);
4 }
5
6 void loop() {
7 // Send a message over the serial port
8 Serial.println("Hello, world!");
9 delay(1000);
10 }
In this code snippet, the setup() function calls the Serial.begin() function, which sets
the baud rate for serial communication. The loop() function uses the Serial.println()
function to send the string "Hello, world!" over the serial port and the delay(1000)
function is used to create a delay of 1 second between each iteration of the loop.
On the other hand, here is an example of Arduino code that uses the Serial library to
receive data from a computer and control an LED:
In this code snippet, the setup() function calls the Serial.begin() function and sets the
baud rate for serial communication and also sets the led pin as output. The loop()
function uses the Serial.available() function to check if there is data available in the
serial buffer, and if so, it reads the data using the Serial.read() function and check if
Satya P. Singh
Electronics Design Workshop
Analog input in Arduino refers to the process of reading an analog voltage value and
converting it into a digital value that can be processed by the microcontroller. The
Arduino board has a built-in analog-to-digital converter (ADC) that can be used to
read analog input.
Here is an example of Arduino code that uses the analogRead() function to read the
voltage on an analog pin and convert it to a digital value:
In this code snippet, the setup() function calls the Serial.begin() function to initialize
the serial communication at 9600 baud rate. The loop() function uses the
analogRead(analogPin) function to read the voltage on pin A0 and store the value in
the sensorValue variable. The Serial.println(sensorValue) function is then used to
send the value over the serial port, where it can be read using a serial monitor or other
serial communication software. The delay(1000) function is used to create a delay of
1 second between each iteration of the loop.
It's important to note that the analogRead() function returns a value between 0 and
1023, where 0 corresponds to 0V and 1023 corresponds to the voltage of the reference
voltage (5V for most boards).
Also, it's important to mention that the number of the analog input pin varies
depending on the type of the board, for example, in Arduino Uno board A0 is the first
analog pin, in Arduino Mega board A0 is the first analog pin and so on.
Satya P. Singh
Electronics Design Workshop
Interrupts in Arduino are a feature that allows the microcontroller to stop executing
the main program and execute a special function called an interrupt service routine
(ISR) when a certain event occurs, such as a button press or a sensor reading. Interrupts
are useful for handling events that need immediate attention and prevent the
microcontroller from wasting time polling for the event to occur.
Here is an example of Arduino code that uses an interrupt to handle a button press:
In this code snippet, the setup() function sets the button pin as an input with a pull-
up resistor enabled, and sets the led pin as an output. The
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButton, FALLING)
function is used to attach the ISR handleButton() to the interrupt of the button pin,
and the FALLING edge of the input is selected as the trigger, this means that the
interrupt will be triggered when the button is pressed and the input goes low. The
loop() function can be used to do something else in the meantime.
The handleButton() function is the ISR and it will be executed when the interrupt is
triggered, in this case it toggles the state of the led pin.
It's also possible to specify different triggers of the interrupt, such as RISING or
CHANGE, and also specify different priorities for the interrupt with the priority
parameter.
It's important to note that the ISR function should be as short as possible and should
not contain any blocking or long-running code, as this can cause the microcontroller
to miss other interrupts or cause the system to become unresponsive.
Satya P. Singh
Electronics Design Workshop
Exercise:
1. What are electronic circuit building blocks, and how are they used to design
complex electronic systems? Explain with the help of suitable examples.
2. Discuss the basics of Arduino programming and its use in electronic projects.
Explain the significance of the Arduino IDE and Basic Sketch structure.
3. How can digital input and output be used in electronic circuits? Provide
examples of applications where digital input and output are used extensively.
4. What are PWM signals, and how are they used to control electronic devices?
Explain with suitable examples.
5. Discuss the importance of serial communication in electronic systems. What are
the different types of serial communication, and how are they used?
6. What is analog input, and how is it used in electronic circuits? Provide examples
of analog input applications.
7. Explain the concept of interrupts programming and its significance in electronic
systems. Provide examples of applications where interrupts are commonly used.
Satya P. Singh