Lab 1
Lab 1
Objectives:
Introduction
An Arduino board consists of an Atmel 8-bit AVR microcontroller with
complementary components to facilitate programming and incorporation
into other circuits.
An important aspect of the Arduino is the standard way that connectors are
exposed, allowing the CPU board to be connected to a variety of
interchangeable add-on modules known as shields.
Official Arduinos have used the megaAVR series of chips, specifically the
ATmega8, ATmega168, ATmega328, ATmega1280, and ATmega2560
Programing
Diital write
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000); // wait for a second
digitalWrite(led, LOW);
delay(1000); // wait for a second
}
Example 2
int pushButton = 2;
void setup() {
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState;
buttonState = digitalRead(pushButton);
Example 3
Read and Write
int pushButton = 2;
int led = 13;
void setup() {
pinMode(pushButton, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int buttonState;
buttonState = digitalRead(pushButton);
if (buttonState==1)
{
digitalWrite(led, HIGH);
}
}
Robotics Lab LAB 1
Example 4
Serial communication
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
Serial.println(buttonState);
delay(1);
}
Example 5
Analouge read
int sensorPin=A0;
int sensorValue=0;
void setup() {
}
void loop() {
sensorValue=analogRead(sensorPin);
}
Example 6
Analouge read with serial
int sensorPin=A0;
int sensorValue=0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue=analogRead(sensorPin);
Serial.println(sensorValue);
delay(1000);
}
Example 7
PWM
int pwmPin = 9; // output pin supporting PWM
void setup()
{
pinMode(pwmPin, OUTPUT); // sets the pin as output
}
void loop()
{
analogWrite(pwmPin, 128);
}
Robotics Lab LAB 1
Example 8
LCD interface
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int sensorPin=A0;
Int sensorValue=0;
void setup()
{
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Voltage : !");
lcd.setCursor(0,1);
sensorValue=analogRead(sensorPin);
lcd.print(sensorValue);}
PWM signal:
PWM, or Pulse Width Modulation, is a widely used technique in electronics and microcontroller
programming to generate analog-like signals from digital devices. PWM signals are digital
waveforms that can simulate varying levels of voltage or brightness by changing the width
(duration) of the ON or HIGH portion of the signal while keeping the voltage level constant during
each cycle.
Duty Cycle: PWM signals have a duty cycle, which is the percentage of time the signal is in the
HIGH state during one cycle. A higher duty cycle corresponds to a higher average voltage, while a
lower duty cycle results in a lower average voltage.
Simulating Analog Values: PWM is often used to simulate analog signals. By rapidly switching a
digital pin ON and OFF with varying duty cycles, you can create the effect of smoothly changing
analog values, such as controlling the speed of a motor, the brightness of an LED, or the position
of a servo motor.
Resolution: The resolution of a PWM signal is determined by the number of discrete steps or
levels that can be represented. Most Arduino boards have 8-bit PWM resolution, meaning they can
produce 256 different output levels (0 to 255).
Frequency: PWM signals have a frequency, which is the rate at which the signal repeats in Hertz
(cycles per second). The frequency determines how quickly the duty cycle can change, affecting
the smoothness of analog-like control.
Applications: PWM signals are used in a wide range of applications, including motor speed
control, LED dimming, audio generation, servo motor control, and more. They provide a flexible
and efficient way to control devices that require variable input.
Arduino and PWM: Arduino microcontrollers, like the popular Arduino Uno, include PWM
functionality on certain pins. You can use the analogWrite() function to generate PWM signals on
these pins. The resolution and frequency may vary depending on the specific Arduino board and
pin being used.
Robotics Lab LAB 1
void setup() {
pinMode(ledPin, OUTPUT);
void loop() {
}
The analogWrite() function in Arduino uses a value between 0 and 255 to control the duty cycle of
a PWM signal. In this context, we are using it to generate a square wave with a specific frequency.
To do this, we need to calculate an appropriate value to set the PWM frequency.
The formula for calculating the PWM value for a desired frequency (in Hz) on an Arduino is:
In the case of most Arduino boards, the PWM resolution is 8 bits, which means resolution is 8. So,
in your example:
Since the PWM value should be an integer between 0 and 255, we round 24.6 down to 24.
Therefore, 500000 is a constant used to calculate the PWM value based on the resolution and the
desired frequency in the given formula. It helps determine the value you should use with
analogWrite() to achieve the closest possible frequency to the desired 5 kHz square wave.
You can generate a square wave using the delay() function in Arduino. Here's an
example code that generates a 1 kHz square wave on Pin 9 with a 50% duty cycle using
delay():
void setup() {
pinMode(squareWavePin, OUTPUT);
}
void loop() {
// Generate a 1 kHz square wave with a 50% duty cycle
digitalWrite(squareWavePin, HIGH);
delayMicroseconds(500); // HIGH for 500 microseconds
digitalWrite(squareWavePin, LOW);
delayMicroseconds(500); // LOW for 500 microseconds
}
Robotics Lab LAB 1
1- A square wave signal is drawn below. Write an Arduino program to obtain this
square wave on pin9. Show the output on the oscilloscope. Connect the LED to
pin9 and observe the output.
2- Two square wave signals are drawn below. Write an Arduino program to obtain
these square waves on pin9 & pin11. Show the outputs on the oscilloscope.
Connect the LEDs to pin9 & pin11 and observe the output.
Robotics Lab LAB 1