• 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.

Need help with controlling an led fading sketch

Thread starter #21
That sounds good.
To hep you locate your code bugs, try using the Serial Monitor and place Serial.prints in your code so you can 'see' whats actually happening as the code runs.


However on your diagram there are problems.

You cannot drive high powered leds directly from the chip / P1, it will simply burn out the chip.

You show Vin connected to the LED driver ? What are you using as the Led Driver ?

A led driver typically uses a control signal from the Micro/ Digispark, often a PWM signal, so it can drive the leds at various brightness.

For your input from the motion detector, the DP should accept 3v3 directly, my Arduino does, however you may want to protect both circuits by using a little opto coupler.
There are many similar devices , not just the one shown in the diagram.
You will probably have to lower the leds input resistor to that shown in the diagram, try 330R if you do not have a multimeter.
Great info! I did consider that putting the supply directly through the Vin would not be feasible, so I do plan to use a PWM capable led driver (I already have it) on a 12v battery circuit to feed the leds via an PNP transistor(?). I will update my schematic and post it soon. I like the idea of incorporating the serial.prints in my code to verify what is happening. I will look into that (tutorials).

One question:
I did decide to swap the function of my code, putting the fade sequence before the delay loop. The code reads pin PO and if high, executes the fade, otherwise, it will show me several 1/2 sec blinks then loop back and start over. I'll change the blink code, when I know the sketch is flowing properly. However, no matter what I do, when I try to verify my sketch, } else { is highlighted and it tells me that there is either 'else without previous if'', or 'expected unqualified-id before '{' token'. I cannot get past it. I have fixed this issue in previous versions of my sketch, but am now stuck.What am I missing?
Code:
/*
 updown_fade

 This example shows how to fade an LED in/out using the analogWrite() function.

 The circuit:
 * LED (+) attached to pin 1. (3.3v w/ resistor)
 * motion sensor 'signal' will be attached to pin 2 (for now I am just attaching and detatching a 3.3v lead to pin2)
 * digispark microcontroller on breadboard (prototyping)

 Created 7/15/2017
 By John A. Austin





 */
int sensorValue = 0; //analog value of sensorPin (P0) when off
int sensorPin = 2;  //motion sensor out pin connected to digital pin 2 (P0 on digispark)
int ledPin = 1;    // LED connected to digital pin 1
int fadeValue = 0; //initial led brightness (variable)
//int analogValue = analogRead(ledPin);

void setup()
{
// initialize the sensorPin as an input:
  pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}





void loop() {


if(digitalRead(sensorPin) == HIGH)
for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1)
{
analogWrite(ledPin, fadeValue);
delay(235);
}
{
analogWrite(ledPin, 0);
delay(2000);
analogWrite(ledPin, 5);
delay(5000);
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 1)
analogWrite(ledPin, fadeValue);
delay(125);
analogWrite(ledPin, 5);
delay(5000);
} else {
analogWrite(ledPin, 255);
delay(500);
analogWrite(ledPin, 0);
delay(500)
loop(5);
}
 
Thread starter #22
As I'm editing my schematic in eagle, I just realized the the analog signal from P1 would only trigger a gate open or closed on the transistor, so I am going straight to a PWM led driver........... I'll post it soon.
 

Wp100

Well-Known Member
#24
Just space out your code into smaller sections, it soon becomes clear where you have gone wrong with your { }

No Expert on Ardunio code but do not know what/how this is used ; it did not build . loop(5);

Had to Upload the code as a .zip file, cannot find the code # tag on the menu bar ???

Edit - the diagram looks ok
 

Attachments

be80be

Well-Known Member
#25
I read this from top and from what your saying?
You want the thing to come on it tripped for 7 minutes then go back off
You really need to have a better switch code
Code:
if (pinInput == 1)
{
  // action A
Run fade code here 
}
else
{
  // action B
Run delay code here 
}
And if you want more to happen add it here.
 
Thread starter #26
Just space out your code into smaller sections, it soon becomes clear where you have gone wrong with your { }

No Expert on Ardunio code but do not know what/how this is used ; it did not build . loop(5);

Had to Upload the code as a .zip file, cannot find the code # tag on the menu bar ???

Edit - the diagram looks ok
Fantastic, I actually went back to a previous, cleaner version of my sketch and have it working now. It is triggering HIGH/LOW and flowing properly as I am watching the led on the controller. I'm trying to get the real world circuit built on the breadboard now (fingers crossed). Thank you for all of your input, it is really helping.....
I read this from top and from what your saying?
You want the thing to come on it tripped for 7 minutes then go back off
You really need to have a better switch code
Code:
if (pinInput == 1)
{
  // action A
Run fade code here
}
else
{
  // action B
Run delay code here
}
And if you want more to happen add it here.
Thanks be80be. Here's the final code and it is working with the sensor really well now. The problem I am having now is figuring out how to connect my LED driver board into the circuit. I will be driving (2) or (3) 3W high power leds ea with a Vf of 2.2v. This particular driver board needs a min 7v apparently. The board has 7 pins.... (2) IN+, (2) IN-, PWM, LED+ & LED-. I cannot find any diagrams of how to connect this board if using the PWM. Here is the board I plan to use. I'm assuming it has a mosfet incorporated somewhere and that maybe I need to add a pull down/up resistor or something to my circuit, but that is over my head as you can imagine. Any ideas?
 

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
#27
What are the numbers on U1? from the markings you can hopefully find the chip number and datasheet and go from there.

Measure the voltage from PWM control to Power In -. Suspect it will be +5.
 
Thread starter #28
Wp100, I finally lookedcat the sketch in post #20. That opto isolator is pretty slick.... my fade sequence seems to be working fine right now as I have a 2N3904 NPN transistor on the neg leg of the leds. It's being switched by the PWM signal from the digispark. I'm now able to run 12v to the leds and control them with low voltage from the controller. I'll let you know if something blows up........:eek:
 
Last edited:

Wp100

Well-Known Member
#29
[QUOTE="hoghunter, post: 1300734, member: 266737"
The problem I am having now is figuring out how to connect my LED driver board into the circuit. I will be driving (2) or (3) 3W high power leds ea with a Vf of 2.2v. This particular driver board needs a min 7v apparently. The board has 7 pins.... (2) IN+, (2) IN-, PWM, LED+ & LED-. I cannot find any diagrams of how to connect this board if using the PWM. Here is the board I plan to use. I'm assuming it has a mosfet incorporated somewhere and that maybe I need to add a pull down/up resistor or something to my circuit, but that is over my head as you can imagine. Any ideas?
[/QUOTE]


The first question to ask is do you have a 1W board or a 3W board ; for your 3W leds you need that latter.

The makers do not seem to publish a datasheet on this board, but if you look at the other drivers on their page they do, so you can see how its done.

The only thing not clear is if a 5v PWM signal is ok for the driver board, often it is.

Any doubts, mail them.

https://www.pcboard.ca/index.php?route=product/search&search=led drivers
 
Thread starter #30
[QUOTE="hoghunter, post: 1300734, member: 266737"
The problem I am having now is figuring out how to connect my LED driver board into the circuit. I will be driving (2) or (3) 3W high power leds ea with a Vf of 2.2v. This particular driver board needs a min 7v apparently. The board has 7 pins.... (2) IN+, (2) IN-, PWM, LED+ & LED-. I cannot find any diagrams of how to connect this board if using the PWM. Here is the board I plan to use. I'm assuming it has a mosfet incorporated somewhere and that maybe I need to add a pull down/up resistor or something to my circuit, but that is over my head as you can imagine. Any ideas?

The first question to ask is do you have a 1W board or a 3W board ; for your 3W leds you need that latter.

The makers do not seem to publish a datasheet on this board, but if you look at the other drivers on their page they do, so you can see how its done.

The only thing not clear is if a 5v PWM signal is ok for the driver board, often it is.

Any doubts, mail them.

https://www.pcboard.ca/index.php?route=product/search&search=led drivers[/QUOTE]

For the initial prototype, I am sending the PWM signal from the MCU's pin 1 to the base of a 2N3904 NPN transistor and letting that allow the ground circuit to open/close on the 12v feeding the leds. I have a 3w constant current led driver on the + side 12v. It seems to be working fine at this point.....
Do you think what I am doing is ok, or am I causing a problem by not using a PWM driver instead of the trans and constant current driver?
 

Latest threads

EE World Online Articles

Loading

 
Top