• Welcome back Guest!

    MARSH is a private reefing group. Comments and suggestions are encouraged, but please keep them positive and constructive. Negative threads, posts, or attacks will be removed from view and reviewed by the staff. Continually disruptive, argumentative, or flagrant rule breakers may be suspended or banned.

Arduino PWM Dimming Example (1 Viewer)

Users who are viewing this thread

ModAquatics

Supporting Member
Joined
Jul 24, 2011
Messages
191
Reaction score
15
Location
Katy, TX
While at FJW's anniversary I told folks about how our lights can do PWM dimming and how they can use an Arduino. Many people didn't know what an Arduino is and others had questions about how it can be used. I just uploaded a video of how to do this with our light engines so I thought that y'all may be interested in the example. Even though I have it plugged into our light engine, y'all can plug it into your PWM drivers such as the Meanwell P series.

The Items used are;

Arduino board - you can use almost any but I use the Arduino UNO SMD
http://www.sparkfun.com/products/10356

Breadboard - such as..
http://www.sparkfun.com/products/112

LCD display - 16x2 Character Parallel display
http://www.sparkfun.com/products/255

Potentiometer -
http://www.radioshack.com/product/index.jsp?productId=2062357

Arduino software -
http://arduino.cc/

The Arduino software is simple to use. There are tutorials on the website and many examples of code that come with the free download. The most basic thing is that the programs are called "Sketches"(Italians :roll: ). The programming language is fairly simple once you understand the syntax. The sketch just simply defines what actions to take when certain inputs happen. The inputs can be a temperature sensor, potentiometer, PH probe, switch, etc. The outputs can be a display, sending power to a relay to turn on a pump, dim an LED, etc. The outputs and inputs can either be digital or analog.

For the purposes of this demonstration we will have analog input from a potentiometer (like a volume knob thing) and the digital outputs will be an LCD display and the LEDs.

All of the pin connections are listed in the Arduino sketch.

Here is a link to the video
http://youtu.be/zbBv7UizGTE

Here is the Sketch

Code:
/*
  ModAquatics Light Engine PWM Demo
 
 Demonstrates the use a PWM Controller interfaced with the 6x12 
 light engine sold by ModAquatics.  It also uses 16x2 LCD display.
  
 This sketch prints "Brightness Level" to the LCD on the first line 
 then the Brightness percentage on the second.
  
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * PWM + to Digital pin 9
 * PWM Ground to Analog Ground
 * Potentiometer wiper to Analog pin 3
 * Potentiometer ends to +5V and ground
 
 
 Example Code from ModAquatics.com
  modified 8-21-2011
  by Robert Hughes
 
 This example code is in the public domain.

  */

// include the library code for the LCD:
#include <LiquidCrystal.h>

int analogPin = 3;       // potentiometer connected to analog pin 3
int val = 0;                // variable to store the read value
int ledPin = 9;            // LED connected to digital pin 9


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // initialize the library with the numbers of the interface pins

void setup() {
    lcd.begin(16, 2);                    // set up the LCD's number of columns and rows: 
    lcd.print("Brightness Level");  // Print Brightness Level to the LCD.
 
    lcd.setCursor(0, 1);               // Move cursor to the next line
 
   pinMode(9, OUTPUT);             // Set the Digital Pin 9 to OUTPUT

}
void loop()  { 
   val = analogRead(analogPin);   // read the input Analog pin 3 values go from 0 to 1023,
   analogWrite(ledPin, val / 4);     //  analogWrite values from 0 to 255 so divide by 4
  lcd.print("   ");                         //Clear the previous percentage
  lcd.setCursor(0, 1); 
  lcd.print((val / 4) / 2.55);         //Print the Brightness Percentage
  delay(100);                             // Wait 100 milliseconds 
}
[/url]
 

KyleH

Guest
Joined
Sep 14, 2010
Messages
221
Reaction score
0
Location
NW Houston (249 & 1960)
just a note, the arduino provides a 0-5V PWM.

Just a note, the Arduino by itself only outputs 0-5V, The meanwells are controlled from a range of 0-10V. This means that you will get about 50% of the maximum output from your meanwells with the Arduino by itself.

Over on reef central, dwzm has a very good and long thread on how to add the extra components to your arduino to control the meanwells. I think the extra components costs only $5-10. total. I had not had a chance to read it all--I'm on page 7 of it, and still a long way to go.

I think one requirement though is that you need to supply an external 10V power supply, and apply a transformer controlled by the arduino. Add a few resistors and you are all set.

If people are planning on using an arduino for led controlling, I would recommend in also looking into the following driver for the arduino that I got with my set up, you will have to provide the power source for the drivers, though: http://www.sureelectronics.net/goods.php?id=1035 . Looks like each $5. piece can control a max of eight 3W LED.

I bought my LED setup from a DIY'er--all that was missing was an arduino that I am working on. I was planning to test mine, to measure the current and voltage for the supplied power (I am thinking about cloning it for a new tank / side project). I suspect you can get a $10. walwart plug or an adjustable one for $15-19.

-KyleH
 

RGH69

Guest
Joined
Jan 2, 2010
Messages
694
Reaction score
0
Location
Katy, Texas
Re: just a note, the arduino provides a 0-5V PWM.

KyleH said:
Just a note, the Arduino by itself only outputs 0-5V, The meanwells are controlled from a range of 0-10V. This means that you will get about 50% of the maximum output from your meanwells with the Arduino by itself.

Hmm, if that is the cae then the Meanwell is not a true PWM driver. PWM ijust tells the driver when to switch the light on and off. PWM does not dictate anything about intensity. I also cannot find anything on Meanwells site stating any voltage requirements for PWM. I wonder if DWZM was using the P-series.
 

KyleH

Guest
Joined
Sep 14, 2010
Messages
221
Reaction score
0
Location
NW Houston (249 & 1960)
Re: just a note, the arduino provides a 0-5V PWM.

I think the way the PWM works in the Arduino is that turns the signal high/low very fast within a period of time. That frequency is measurable and translated into a range of 0-255 that you can program with the analogWrite (as you used in your example).

What I am not sure is how the combined value or sum is interpreted with the arduino's analogRead to get the values 0-255. Does that mean a value of 128 is 2.5V or 64 is 1.25V? Another person in that thread suggested that if you provide the max of the 5V in the analogWrite, the meanwell may think it is only 50% of the maximum available and dim it by 50%. It may be worthwhile to measure it with a voltmeter...but, heck, I am a lazy guy, so I will do it "tomorrow".

From reading off of dwzm's thread on reefcentral, they are using a LM317 with the arduino to control the power from an external 10V source: http://www.reefcentral.com/forums/showthread.php?t=1793374

From reading, I think dwzm was using a type-P. He was looking for volunteers to try out the type-D to test his setup. It is a long thread, I don't know where I got the impression of a 0-10V on the meanwells. I will try to take a look at it later when I get home from work.

-KyleH

RGH69 said:
Hmm, if that is the cae then the Meanwell is not a true PWM driver. PWM ijust tells the driver when to switch the light on and off. PWM does not dictate anything about intensity. I also cannot find anything on Meanwells site stating any voltage requirements for PWM. I wonder if DWZM was using the P-series.
 

DustinB

Guest
Joined
Jun 2, 2010
Messages
874
Reaction score
0
Location
Santa Fe
While I haven't read dwizum's example, he is likely just using a transistor controlled by an arduino pin. The transistor will vary the voltage of the 10v power supply. As for the supply, personally I would just use the LM317T to bring the voltage down to 5v for the arduino so you can use a single 10v power supply. You may need a heatsink, but the power draw from the arduino is fairly small anyway.

Pretty sure the meanwell is just 0-10v controll, not PWM. As for PWM, the voltage does stay the same, just the amount of of time the signal is high varies.

If I'm not mistaken, you should be able to use PWM (analog output) from 0-255 as you stated to control the transistor varying the 10v supply.

PWM explanation: http://www.arduino.cc/en/Tutorial/PWM
 

RGH69

Guest
Joined
Jan 2, 2010
Messages
694
Reaction score
0
Location
Katy, Texas
Hello Dustin,

Are you doing anything with LED anymore? I remember you had it on a BC29 but I thought you sold it.
 

DustinB

Guest
Joined
Jun 2, 2010
Messages
874
Reaction score
0
Location
Santa Fe
I had an LED setup retro'd into a current usa fixture over a 29g biocube, I did sell the whole setup several months back.

I did an LED setup for my display refugium recently, but other than that I don't have any more plans for LEDs. Just sticking with T5 for now, but I'm considering switching back to metal halide.
 

pelochas

Guest
Joined
Oct 12, 2005
Messages
390
Reaction score
0
Location
Houston, Texas, United States
this looks so easy yet im having trouble understanding why i cant get this to dim my leds

i have a cat4101 driver. needs only 5v PWM

I have just the arduino uno board and the 16x2 lcd display

after i have connected it all up using the my first sketch "hello world" it works fine, i get the display hello world and seconds counting up. i adjust the pot, the lcd changes the contrast.

ok so now i move on to this sketch, i see i need to move a connection here, add this connection. upload the sketch. i get something similiar. i get the "brightness displayed" and the percentage. when i adjust the pot, the lcd dims instead of the percentage of brightness.

i guess i have some connections in the wrong area or not clear what im doing wrong.
 

RGH69

Guest
Joined
Jan 2, 2010
Messages
694
Reaction score
0
Location
Katy, Texas
It sounds like you have the Pot wiper connected to the lcd instead of pin 3. You can just add a second pot, hook one end to 5v, the other end to ground and the middle to pin 3. If that does not work, PM me your number and I can call to help troubleshoot.
 

RGH69

Guest
Joined
Jan 2, 2010
Messages
694
Reaction score
0
Location
Katy, Texas
DustinB said:
While I haven't read dwizum's example, he is likely just using a transistor controlled by an arduino pin. The transistor will vary the voltage of the 10v power supply. As for the supply, personally I would just use the LM317T to bring the voltage down to 5v for the arduino so you can use a single 10v power supply.

I actually did this for a customer that already had a diy LED array. I used 10v supply to power the Arduino (most can have input up to 12v) and the transistor/Meanwell P driver input.
 

pelochas

Guest
Joined
Oct 12, 2005
Messages
390
Reaction score
0
Location
Houston, Texas, United States
ok i got your help here and at RC.

added

Code:
#include LiquidCrystal.h

Code:
lcd.setCursor(0, 1); // Move cursor to the next line 
pinMode(3, INPUT); // Set the Digital Pin 9 to OUTPUT
pinMode(9, OUTPUT); // Set the Digital Pin 9 to OUTPUT

so uploading it shows the the level percentage 0 - 100 when i adjust the pot

now the big question, the pwn connection to my driver is the digital out 9?

and eventually i will make this do two pwm's for blue and white leds
 
OP
OP
ModAquatics

ModAquatics

Supporting Member
Joined
Jul 24, 2011
Messages
191
Reaction score
15
Location
Katy, TX
Yes, the LED output in the example above is pin 9. On the Arduino Uno pin 9 is PWM. I also have code to do multiple channels sunrise, sunset, moon settings with a manual override. I will post it on my forum in the next day or so.
 

RGH69

Guest
Joined
Jan 2, 2010
Messages
694
Reaction score
0
Location
Katy, Texas
flexrac said:
please speak english, LOL!

Perhaps I could hold an Arduino workshop or something like that for the club. If so, maybe Dustin or KyleH could also help lead the workshop.
 

flexrac

Guest
Joined
Mar 19, 2009
Messages
5,222
Reaction score
14
Location
Remington Ranch
that would be cool, although i don't have anything to control right now. all of my leds are either constant power or controlled by a pot. no pwm.
 

KyleH

Guest
Joined
Sep 14, 2010
Messages
221
Reaction score
0
Location
NW Houston (249 & 1960)
have you tried it without the led?

Have you tried it without the lcd? The LCD is complex, so you may want to try a simple wiring without it.

All you need is to take one of the digital pins, send an analogwrite range from 0-255, and that should specify an intensity.

I think when I tried on my led, I did not get any visible lighting from 0-50, so you may want to try 128, 168, 200 with a pause of 1-3 seconds to see if your arduino is working.

Also, most setups require a common ground, so you will have to wire that to the arduino.

If you have problems sending a 128 signal with the analogwrite, how about posting your code and picture/drawing-sketch of your wiring. (If you think I can help directly, and need my attention, send me a quick PM to check this thread...I have been lazy lately).

< I attached a simple example that I used. It starts at 0, waits 1 second, then tries 20, wait 1 second, then try 40, etc. resets to 0 after it exceeds 200. It is wired to pin 10. >

-Kyle



pelochas said:
this looks so easy yet im having trouble understanding why i cant get this to dim my leds

i have a cat4101 driver. needs only 5v PWM

.....
 

Scott

Guest
Joined
Sep 3, 2007
Messages
2,032
Reaction score
0
Location
Webster
Re: just a note, the arduino provides a 0-5V PWM.

KyleH said:
Just a note, the Arduino by itself only outputs 0-5V, The meanwells are controlled from a range of 0-10V. This means that ...
For clarification on this, the Arduino outputs a 5V signal but the input range should be between 7V&12V, while it can vary up to 20V it should be kept as close to 10V as possible. That being said you can use the same 10V wallwart to supply power to your MW driver and the Arduino.

Now for a quick lesson on dimming:

The best (maybe not the easiest) way to utilize pulse width modulation with the arduino is to use the map call for arduino programming to give the 0-255 pwm signal a 0-100% range, that should help also

map example: y = map(x, 1, 50, 50, 1);

- OR -

y = map(x, 0, 100, 0, 255) where Y is the defined variable in the function call, X is the new definition of that variable, 0 is the USER DEFINED LOW and 100 is the USER DEFINED HIGH while 0 is the PWM DEFINED LOW and 255 is the PWM DEFINED HIGH

So when the user says to go to a value of 100 the PWM signal goes to 255 and when the user says go to a value of 50 the PWM goes to 127 (the .5 is truncated)
 

pelochas

Guest
Joined
Oct 12, 2005
Messages
390
Reaction score
0
Location
Houston, Texas, United States
thanks for the help and info. im able to dim the leds. is pwm votlage variable with the pot? i thought the pwm is 5v all time but the rapid on-off rapid signal is what dims the leds. need to read more
 

KyleH

Guest
Joined
Sep 14, 2010
Messages
221
Reaction score
0
Location
NW Houston (249 & 1960)
there are 2 approaches with pot. (1) You can assign a separate pin and use that pin to read the signal on the pot. Based on the levels, you can write to the a separate pin controlling your LED. This way, I think the internal pulsing of the PWM will adjust the frequency. The changed frequency will reflect the 0-255 levels that you chose in your analogWrite.

(2) I am not 100% sure, I have not tried it in a long time (my background is programming, and less on the electronics side of things), but I *think* you can put the pot on the same line as pwm. As such, it serves as variable resistor, so the output signal varies from 0-5V. Your drivers may be able to detect voltage and/or the frequency. In cases when you are not using a PWM driver, you may need to use this approach. In other cases, I think you can use a 5V PWM controlled digital pot.

My understanding is the PWM on single dumb led in the arduino examles is that it pulses 0V to 5V depending on frequency, and the amount of electricity is sent to give it a variable effect from the pulsing. Also, you can see the same effect when you use different kinds of resitors or a POT without changing the PWM pulsing on the same dumb led. I think that is what is happening.

-KyleH


pelochas said:
thanks for the help and info. im able to dim the leds. is pwm votlage variable with the pot? i thought the pwm is 5v all time but the rapid on-off rapid signal is what dims the leds. need to read more
 
Top