In this demonstration we will going to drive the LED on breathing / pulsing effect simply turn the LED on or off while adjusting the brightness.
Required Parts
Arduino UNO/MEGA/NANO/PRO
LED Any cor
220 k Ohms Resistor
Wiring the LED to Arduino
The Arduino Sketch Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* 14CORE Pulsing / Breating LED Sketch */ int LEDPin = 11; float sinVal; int ledVal; void setup() { pinMode(ledPin, OUTPUT); } void loop() { for (int x=0; x<180; x++) { // convert degrees to radians then obtain the sin value then obtain sin value sinVal = (sin(x*(3.1412/180))); /* sin() function, which is a mathematical function to work out the sine of an angle. We need to give the function the degree in radians. We have a for loop that goes from 0 to 179, we donʼt want to go past halfway as this will take us into negative values and the brightness value we need to put out to Pin 11 needs to be from 0 to 255 only. The sin() function requires the angle to be in radians and not degrees so the equation of x*(3.1412/180) will convert the degree angle into radians. We then transfer the result to ledVal, multiplying it by 255 to give us our value. The result from the sin() function will be a number between -1 and 1 so we need to multiply that by 255 to give us our maximum brightness. We ʻcastʼ the floating point value of sinVal into an integer by the use of int() in the statement ledVal = int(sinVal*255); Then we send that value out to Digital Pin 11 using the statement */ ledVal = int(sinVal*255); analogWrite(ledPin, ledVal); delay(25); // Delay 2 seconds } } |
Breathing / Pulsating LED with Arduino
This code doesn’t compile, it contains an error and an incorrect comment:
The error:
– It define LEDPin but then refers to ledPin. Replace one or the other to agree with the other one.
The Incorrect comment:
– The “delay(25)” does not pause for two seconds. The parameter to delay() is in milliseconds. it should be delay(2000)
None of this is really hard to fix but you may want to correct the code.
Thank you