[go: up one dir, main page]

0% found this document useful (0 votes)
10 views4 pages

Tutorial Sheet 8 Sensor Unit 4

Uploaded by

utsavmukherjee12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Tutorial Sheet 8 Sensor Unit 4

Uploaded by

utsavmukherjee12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial Sheet 8- (Sensor)

Question-1 Write down a program for blinking of LED.

Code

After you build the circuit plug your Arduino board into your computer, start the Arduino Software
(IDE) and enter the code below. You may also load it from the menu
File/Examples/01.Basics/Blink . The first thing you do is to initialize LED_BUILTIN pin as an
output pin with the line
pinMode(LED_BUILTIN, OUTPUT);
In the main loop, you turn the LED on with the line:
digitalWrite(LED_BUILTIN, HIGH);
This supplies 5 volts to the LED anode. That creates a voltage difference across the pins of the
LED, and lights it up. Then you turn it off with the line:
digitalWrite(LED_BUILTIN, LOW);
That takes the LED_BUILTIN pin back to 0 volts, and turns the LED off. In between the on and the
off, you want enough time for a person to see the change, so the
delay()
commands tell the board to do nothing for 1000 milliseconds, or one second. When you use the
delay()
command, nothing else happens for that amount of time. Once you've understood the basic examples,
check out the BlinkWithoutDelay example to learn how to create a delay while doing other things.
// the setup function runs once when you press reset or power the board
--------------------------------------
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
----------------------------------------------------------------------------
Question-2 Write down a program to interface Arduino with LDR.
const int ledPin = 13; //pin at which LED is connected
const int ldrPin = A0; //pin at which LDR is connected
int threshold = 118;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //Make LED pin as output
pinMode(ldrPin, INPUT); //Make LDR pin as input
}
void loop()
{
//analogWrite(9,(1023-analogRead(ldrPin)));
//delay(100);
int ldrStatus = analogRead(ldrPin); //saving the analog values received from LDR into "ldrStatus"
variable
if (ldrStatus <= threshold) //set the threshold value below at which the LED will turn on
{ //you can check in the serial monior to get approprite value for your LDR
digitalWrite(ledPin, HIGH); //Turing LED ON
Serial.print("Its DARK, Turning ON the LED : ");
Serial.println(ldrStatus);
}
else
{
digitalWrite(ledPin, LOW); //Turing OFF the LED
Serial.print("Its BRIGHT, Turning OFF the LED : ");
Serial.println(ldrStatus);
}
}

Question-3 Write down a program to interface Arduino with Ultrasonic Sensor.


/*
Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the distance
to the closest object in range. To do this, it sends a pulse to the sensor to
initiate a reading, then listens for a pulse to return. The length of the
returning pulse is proportional to the distance of the object from the sensor.

The circuit:
- +V connection of the PING))) attached to +5V
- GND connection of the PING))) attached to ground
- SIG connection of the PING))) attached to digital pin 7

created 3 Nov 2008


by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping
*/

// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance


inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds) {


// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}

Home work:
1. How to play a melody using tone function?

Play a Melody using the tone() function | Arduino Documentation

2. How to use a variable resistor (a potentiometer or a photoresistor) using


Arduino board?

Analog Input | Arduino Documentation

You might also like