Deryoung Electronics
         arduino lab note
 
Welcome to ... Guest! Not yet Open Comming Soon | Login

Play with LED



   

1. Input Logic "1" to Digital pin 9

//*************************************
//Program:ONled.pde
//Crate by:Deryoung.com
//Resulte: Input Logig "1" to LED
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
void setup()
{
  pinMode(ledPin,OUTPUT);
}

void loop() 
{
  digitalWrite(ledPin,HIGH);
}


Figure 1. this picure is result of above program ONled.pde

2. "blink" LED at digital 9

//*************************************
//Program:blinkLED.pde
//Crate by:Deryoung.com
//Resulte: brinking LED
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
void setup()
{
  pinMode(ledPin,OUTPUT);
}

void loop() 
{
  digitalWrite(ledPin,HIGH);
  delay(50);
  digitalWrite(ledPin,LOW);
  delay(50);
  
}

3. Learn Digital Read command   (LED OFF with input Logic "1")

//*************************************
//Program:offLEDwithSW.pde
//Crate by:Deryoung.com
//Resulte: LED will be "OFF" state when Microswitch is pressed.
//   Note: After press SW. logic "1" assign to digital-7
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
int microSwitch=7;
int val=0;

void setup()
{
  pinMode(ledPin,OUTPUT);
  pinMode(microSwitch,INPUT);
}

void loop() 
{
  val=digitalRead(microSwitch);
  if(val==HIGH){
    digitalWrite(ledPin,LOW);
  }
  else {
    digitalWrite(ledPin,HIGH);
  }
}

4. Learn Digital Read command   (LED OFF with input Logic "0")

//*************************************
//Program:offLEDwithSW2.pde
//Crate by:Deryoung.com
//Resulte: LED will be "OFF" state when Microswitch is pressed.
//   Note: After press SW. logic "0" assign to digital-7
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
int microSwitch=7;
int val=0;

void setup()
{
  pinMode(ledPin,OUTPUT);
  pinMode(microSwitch,INPUT);
  digitalWrite(ledPin,HIGH); // defalut LED is "ON"
}

void loop() 
{
  val=digitalRead(microSwitch);
  if(val==LOW){ //<<--- connect digital-7 to GND for OFF LED
    digitalWrite(ledPin,LOW);
  }
  else {
    digitalWrite(ledPin,HIGH);
  }
}

Notice:

1. At the first time, do not connect anything to digital-7.
2. Use DC supply from USB port only .
3. Upload the above program to board.
4. LED is OFF, Why ?
5. Let a short copper wire put its into digital-7 and observe at LED ON or OFF.
6. My answer is ON.

7..My question is Do you need to use micro switch and what is your idea?

5.LED Dimmer

//*************************************
//Program:LEDdimmer.pde
//Crate by:Deryoung.com
//Resulte: LED dimmer using variable Resister.
//Interface: LED --> digital-9 and GND.
//           VR1: Pin middle -->anaglog-0,
//                Pin left --> + 
//                Pin right --> Gnd.
//*************************************

int ledPin=9; //PWM
int VR_inputPin=0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop()
{
 int val=analogRead(VR_inputPin);
  val=map(val,0,1023,0,255);
  Serial.println(val);
  analogWrite(ledPin,val);
}


Note:
For digital-9,digital-10,digital-11,digital-9,digital-6,digital-5 and digital-3 as PWM (Pulse Width Modulation) digital output.
the result will bring smooth dim the LED light well.

val
analogRead(VR_inputPin)
LED
0
.
.
100
.
.
254

0
.
.
500
.
.
1023
dark
.
.
medium
.
.
Highest bright

Figure 5. show dump screen from Serial Monitor

Question: What are happen if you use digital-12 connect to your LED.

Answer : See note

6. LDR detect the light

//*************************************
//Program:DetectLight.pde
//Crate by:Deryoung.com
//Resulte: Learning what happening when we pass over the LDR?
//Interface: LED --> digital-9 and GND.
//Important Note: Power positive(+) will be connect to LDR
//                Power negative(-) will be connect o Resistor (8.1K).
//                Midle pin (R and LDR) will be connect to analog-0 pin 
//  and Please open "Serial Monitor" and see value (val) on its.
//*************************************
int ledPin=9; //PWM
int VR_inputPin=0;
int threshold=100; // please try chang here!
void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(57600);
}

void loop()
{
  int val=analogRead(VR_inputPin);
  val=map(val,0,1023,0,255);
  Serial.println(val);
 
  if(val<threshold) //you can change this value.
  {
     Serial.println("shadow");//can be delete.
     digitalWrite(ledPin,LOW);
     delay(1000); //LEis "off" 1 second
  }
  analogWrite(ledPin,val);
}


Figure 5.1 Simplest LDR circuit for detector.

This program is an example for learning how to detect the light with LDR and show output on 2 ways
1. Output on LED at connect to digital pin 9.
2. See the changing value on "Serial monitor" in Aduino API program.

this program we can change the line
.
.
int threshold=100; // please try chang here!
.
.

Note:
1. We can set threshold value from 0-255 for find the best sentivity. After you upload program you must open the "Serial Monitor" and set speed to 57600. and see the value on the screen that changing.
2.Pass your hand top over the LDR and see value on screen again.
3.Remember a value where your hand over the LDR ,
    bring this value replace value on "int threshold= ???"

4.the good result should display "shadow" when your hand top over the LDR.


val
Environment
107 (max)
.
.
26
day
.
.
night
Figure 5.2 value of "val" with the environment light

volt (out)
Environment
2.0v
.
.
~1.3v
.
.
0.3v
day
.
.
shadow
.
.
night
Figure5.3 value of volt when the shadow pass to LDR
compare between GND and "out" pin.

 
This website is personally and all reserved right of meterial, you can copy and use its with refer link to this site
Email contact : support@deryoung.com