Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

i need a circuit and code to generate pwm

Status
Not open for further replies.

formerX

New Member
i need a circuit and code to generate pwm.
i have a code to generate pwm.

Arduino - Fading

i get fading of led's and its shows pwm.
its showing dimmming light and some point it shut off.
but i need to make it as an on and off.
this is my code to make it on and off through pwm.

Code:
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9

void setup()
{
// nothing for setup
}

void loop()
{
for(value = 0 ; value <= 174; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 174; value >=255; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
}
but stilll it is not working.
can u help me out.
 
Last edited by a moderator:
If you want your LED to fade all the way in and out you should change 174's to 255. Also in your 2nd for satment you need to change 255 to 0 so your logic makes sence. Since you initalize your value in each for ststment you don't need to initalize it in your variable declaration but that's just a clean code issue. One last thing I'm not sure if you copied the code incompletely but there should be one more } at the end.

-Wayne
 
void setup() needs another line:

Code:
pinMode(ledPin, OUTPUT);
 
Last edited:
This code looks incorrect:

Code:
for(value = 174; value >=255; value-=5) // fade out (from max to min)

change to:

Code:
for(value = 174; value >= 0; value-=5) // fade out (from max to min)
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top