Authorized Online Retailers:
In this lesson, we will show how to gradually change the luminance of an LED through programming. Since the pulsing light looks like breathing, we give it a magical name – breathing LED. We’ll accomplish this effect with pulse width modulation (PWM).
Arduino IDE (version 1.6.4+)
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.
To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5V controlling the brightness of the LED. (See the PWM description on the official website of Arduino).
We can see from the top oscillogram that the amplitude of the DC voltage output is 5V. However, the actual voltage output is only 3.75V through PWM because the high level only takes up 75% of the total voltage within a period.
Here is an introduction to the three basic parameters of PWM:
There are 5 pins on most OSOYOO Basic Boards marked with ‘PWM’ next to the pin number (on some boards it is an “~” symbol) – these pins are PWM pins.We use the pin3 to control the LED here.Connect the hardware as below Fritzing digram:
Notice:
You cannot directly connect an LED to a battery or voltage source. Firstly, because the LED has a positive and a negative lead and will not light if they are the wrong way around and secondly, an LED must be used with a resistor to limit or ‘choke’ the amount of current flowing through the LED – otherwise the LED could burn out!
Connect the OSOYOO Basic Board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and select corresponding board type and port type for your OSOYOO Basic Board.
Now load the ‘PWM_Control_LED’ example sketch or copy below code to your new Arduino IDE window and upload it to your OSOYOO Basic Board. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.
void setup () { pinMode(3,OUTPUT);// declare pin 3 to be an output } void loop() { for (int a=0; a<=255;a++) //loop from 0 to 255,it controls the increase in PWM brightness { analogWrite(3,a); //set the brightness of pin 3: delay(8); //wait for 8 ms } for (int a=255; a>=0;a--) //loop from 255 down to 0,it control PWM brightness reduction { analogWrite(3,a); // set the brightness of pin 3: delay(8); //wait for 8 ms } delay(800); //wait for 800 ms }
A few seconds after the upload finishes, you should see the LED gets brighter and brighter, and then slowly dimmer, just like breathing.
DownLoad Url osoyoo.com
You must be logged in to post a comment.
try this Sketch (adds the built-in LED in case someone doesn’t get the breadboard hookup correct):
void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT); // declare pin 3 to be an output
pinMode(LED_BUILTIN, OUTPUT); // pin 13 on an Uno R3
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, HIGH); // LED on
for (int a=0; a=0;a–) //loop from 223 down to 0, it control PWM brightness reduction
{
analogWrite(3,a); // set the brightness of pin 3:
delay(4); // wait for 4 ms
}
delay(300); //wait for 300 ms
}
hmm.. some of the code vanished due to the use of (less than) ” (greater than) chars. this message will test
try this instead (make the substitutions for .lt. and .gt. chars below):
void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT); // declare pin 3 to be an output
pinMode(LED_BUILTIN, OUTPUT); // pin 13 on an Uno R3
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, HIGH); // LED on
for (int a=0; a.lt.=223;a++) //loop from 0 to 223,it controls the increase in PWM brightness (max 255)
{
analogWrite(3,a); //set the brightness of pin 3:
delay(4); //wait for 4 ms
}
digitalWrite(LED_BUILTIN, LOW); // LED off
for (int a=223; a.gt.=0;a–) //loop from 223 down to 0, it controls PWM brightness reduction
{
analogWrite(3,a); // set the brightness of pin 3:
delay(4); //wait for 4 ms
}
delay(300); //wait for 300 ms
}