CONTROLLING A LED AND A SPEAKER WITH LDR
CODE LDR CONTROL LED
const int ledPin = 13; // LED connected to digital pin 13
const int sensorPin = 0; // connect sensor to analog input 0
// the next two lines set the min and max delay between blinks
const int minDuration = 100; // minimum wait between blinks
const int maxDuration = 1000; // maximum wait between blinks
void setup(){
pinMode(ledPin, OUTPUT); // enable output on the led pin
}
void loop(){
int rate = analogRead(sensorPin); // read the analog input
// the next line scales the blink rate between the min and max values
rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);
}
LDR CONTROL A SPEAKER
Code:
const int ledPin = 13; // LED connected to digital pin 13
const int sensorPin = 0; // connect sensor to analog input 0
// the next two lines set the min and max delay between blinks
const int minDuration = 100; // minimum wait between blinks
const int maxDuration = 1000; // maximum wait between blinks
void setup(){
pinMode(ledPin, OUTPUT); // enable output on the led pin
Serial.begin(9600); // initialize Serial
}
void loop(){
int rate = analogRead(sensorPin); // read the analog input
// the next line scales the blink rate between the min and max values
rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
Serial.println(rate); // print rate to serial monitor
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);
}
SPEAKER and Tone
Use the Arduino tone function. This sketch plays a tone with the frequency set by a
variable resistor (or other sensor) connected to analog input 0 (see Figure 9-3):
/**Tone sketch
Plays tones through a speaker on digital pin 9 frequency determined by values read from analog port*/
const int speakerPin = 9; // connect speaker to pin 9
const int pitchPin = 0; // pot that will determine the frequency of the tone
void setup(){}
void loop(){
int sensor0Reading = analogRead(pitchPin); // read input to set frequency
// map the analog readings to a meaningful range
int frequency = map(sensor0Reading, 0, 1023, 100,5000); //100Hz to 5kHz
int duration = 250; // how long the tone lasts
tone(speakerPin, frequency, duration); // play the tone
delay(1000); //pause one second
}
The light intensity received in a LDR (light dependent resistor) controlled intensity in a LED-RGB (LED with red, green and blue colours). Arduino is the controller.
Source code:
int LDRvalue = 1;
int RedLED = 9;
int GreenLED=10;
int BlueLED=11;
int pinLDR = 1;
//RGB LED uses 3 outputs to control 3 basic colours
void setup()
{
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
analogReference(EXTERNAL);
//Pins are outputs
}
void loop()
{
LDRvalue = analogRead(pinLDR);
if(LDRvalue >= 1023)
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
// Value corresponds to Colour Intensity
}
else if((LDRvalue >= 959) & (LDRvalue < 1023))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 895) & (LDRvalue < 959))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 831) & (LDRvalue < 895))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 767) & (LDRvalue < 831))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 703) & (LDRvalue < 767))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 639) & (LDRvalue < 703))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 575) & (LDRvalue < 639))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 511) & (LDRvalue < 575))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 447) & (LDRvalue < 511))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 383) & (LDRvalue < 447))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 319) & (LDRvalue < 383))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 255) & (LDRvalue < 319))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 191) & (LDRvalue < 255))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 127) & (LDRvalue < 191))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 63) & (LDRvalue < 127))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >=0) & (LDRvalue < 63))
{
digitalWrite(RedLED, 55);
digitalWrite(GreenLED, 55);
digitalWrite(BlueLED, 55);
}
else
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
}
}
void color(int red, int green, int blue)
{
analogWrite(RedLED, 255-red);
analogWrite(GreenLED, 255-green);
analogWrite(BlueLED, 255-blue);
// The conditional if-else structure controls colour corresponding to light intensity.
}
/*
* Speaker sketch with photoresistor
*/
const int outputPin = 9; // Speaker connected to digital pin 9
const int sensorPin = A0; // connect sensor to analog input 0
const int low = 200;
const int high = 800;
const int minDuration = 1; // 1 ms on, 1 ms off (500 Hz)
const int maxDuration = 10; // 10 ms on, 10 ms off (50 Hz)
void setup()
{
pinMode(outputPin, OUTPUT); // enable output on the led pin
}
void loop() {
int sensorReading = analogRead(sensorPin); // read the analog input
int delayval = map(sensorReading, low, high, minDuration, maxDuration);
delayval = constrain(delayval, minDuration, maxDuration);
digitalWrite(outputPin, HIGH); // set the pin on
delay(delayval); // delay is dependent on light level
digitalWrite(outputPin, LOW); // set the pin off
delay(delayval);
}
//LDR controlling RGB LED and buzzer
int valueLDR, freq;
int ledRed=9;
int ledGreen=10;
int ledBlue=11;
int pinLDR=A0;
int speaker=6;
void setup() {
// put your setup code here, to run once:
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
//analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop() {
Serial.println(valueLDR);
delay(1);
// put your main code here, to run repeatedly:
valueLDR=analogRead(pinLDR);
int Red = map(valueLDR,50,500,0,255);
int Green = map(valueLDR,50,500,255,0);
int Blue = map(valueLDR,50,500,0,128);
int freq = map(valueLDR,50,500,261,494);
analogWrite(ledRed,Red);
analogWrite(ledGreen,Green);
analogWrite(ledBlue,Blue);
tone(speaker,freq,10);
}
LDR and neopixel simulation
LDR and RGBLED simulation
https://www.tinkercad.com/things/eoNwqEgf5Hq-copy-of-led-rgb-ldr