0% found this document useful (0 votes)
47 views

Task 1: Simple Light Reading With LDR: Schematic

The document contains code for 5 tasks that involve reading sensor input and controlling outputs like LEDs. Task 1 reads the value of a light dependent resistor and prints it out. Task 2 uses a light dependent resistor to control an LED, turning it on when the reading is below a threshold. Task 3 uses a potentiometer to control the brightness of an LED. Task 4 uses a potentiometer to light up 5 LEDs one by one at a brightness and delay determined by the sensor reading. Task 5 shows code to display numbers on a 7-segment LED display by turning on the appropriate segments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Task 1: Simple Light Reading With LDR: Schematic

The document contains code for 5 tasks that involve reading sensor input and controlling outputs like LEDs. Task 1 reads the value of a light dependent resistor and prints it out. Task 2 uses a light dependent resistor to control an LED, turning it on when the reading is below a threshold. Task 3 uses a potentiometer to control the brightness of an LED. Task 4 uses a potentiometer to light up 5 LEDs one by one at a brightness and delay determined by the sensor reading. Task 5 shows code to display numbers on a 7-segment LED display by turning on the appropriate segments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Task 1: Simple Light Reading With LDR

Schematic

int LDR_Pin = A0; //analog pin 0


void setup(){
Serial.begin(9600);
}
void loop(){
int LDRReading = analogRead(LDR_Pin);
Serial.println(LDRReading);
delay(250); //just here to slow down the output for easier reading

Task 2: Controlling an LED using a Light Dependent


Resistor (LDR)
Schematic

CODE
int sensorReading;//analog pin reading
void setup()
{
Serial.begin(9600);
pinMode(6,OUTPUT);

}
void loop()
{
sensorReading=analogRead(0); //get analog reading
if (sensorReading<700)
{
digitalWrite(6,HIGH);
}
else digitalWrite(6,LOW);
Serial.println(sensorReading);
delay(1000);
}

Task 3:Analog In, Out Serial (Potentiometer And LED )


Schematic

CODE
const int analogInPin = A0; // Analog input pin that the
potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is
attached to
int sensorValue = 0;
int outputValue = 0;

// value read from the pot


// value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:

delay(2);
}

Task 4:Arduino potentiometer & 5 LED


Schematic

Code
int sensorValue = 0; //make a variable where you can store
incoming
//analog values
void setup(){
pinMode(12, OUTPUT); //tell arduino what you'll be using these
pins
pinMode(11, OUTPUT); // for (output).
pinMode(10, OUTPUT);

pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
Serial.begin(9600); //initialize serial
}
void loop(){

//we put the code we want executed in a loop

Serial.print("sensor = " ); //sends what's in quotes via serial


Serial.println(sensorValue); //sends our variable (sensorValue)
//via serial
digitalWrite(12,HIGH);
// lights the led
sensorValue = analogRead(0); // reads pin 0
delay(sensorValue + 25);
// sensorValue used for delay
digitalWrite(12,LOW);
//turns off the led
delay(15);
//delay before moving to next output pin
//the + 25 keeps delay from reaching zero
//code below is for remaining 4 LEDs
digitalWrite(11,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(11,LOW);
delay(15);
digitalWrite(10,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(10,LOW);
delay(15);
digitalWrite(9,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(9,LOW);
delay(15);
digitalWrite(8, HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);

digitalWrite(8, LOW);
delay(15);
digitalWrite(9,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(9,LOW);
delay(15);
digitalWrite(10,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(10,LOW);
delay(15);
digitalWrite(11,HIGH);
sensorValue = analogRead(0);
delay(sensorValue + 25);
digitalWrite(11,LOW);
delay(15);
}

Task 5:Driving a 7-Segment LED Display

Wiring Input/Output
A= Pin 2
B= Pin 3
C= Pin 4
D= Pin 6
E= Pin 7
F= Pin 8
G= Pin 9

Code

// bits representing segments A through G (and decimal point) for numerals 0-9
const byte numeral[10] = {
//ABCDEFG /dp

~B11111100,
~B01100000,
~B11011010,
~B11110010,
~B01100110,
~B10110110,
~B10111110,
~B11100000,
~B11111110,
~B11100110,
}
;

//
//
//
//
//
//
//
//
//
//

0
1
2
3
4
5
6
7
8
9

// pins for decimal point and each segment


// dp,G,F,E,D,C,B,A
const int segmentPins[8] = { 5,9,8,7,6,4,3,2};
void setup()
{
for(int i=0; i < 8; i++)
{
pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
}
}
void loop()
{
for(int i=0; i <= 10; i++)
{
showDigit(i);
delay(1000);
}
// the last value if i is 10 and this will turn the display of
delay(2000); // pause two seconds with the display of
}
// Displays a number from 0 through 9 on a 7-segment display
// any value not within the range of 0-9 turns the display of
void showDigit( int number)
{
boolean isBitSet;
for(int segment = 1; segment < 8; segment++)
{
if( number < 0 || number > 9){
isBitSet = 1; // turn of all segments
}

else{
// isBitSet will be true if given bit is 1
isBitSet = bitRead(numeral[number], segment);
}
isBitSet = ! isBitSet; // remove this line if common cathode display
digitalWrite( segmentPins[segment], isBitSet);
}
}

You might also like