Go Down

Topic: control the led voltage using pwm pin in arduino (Read 3485 times) previous topic - next topic

Jack-kay

May 25, 2016, 08:10 am Last Edit: May 25, 2016, 04:08 pm by Jack-kay
I have some difficulties using PWM pin. I start using simulation by using proteus. it's just a simple coding to stable the voltage from PWM pin in Arduino. I used RC filter to stable the voltage of the PWM. I used 5 LED and 5 PWM pin that is pin 5, 6, 9, 10, and 11. the problem that occurs is the when I do the simulation pin 5 and 6 do not give any voltage output. I try to insert 255 for each pin and it shows the stable voltage. however, I do not want to use maximum voltage. I try by using other value but pin 5 and 6 does not give any result. please give some opinion... I need help from you all... this is my starting point... if I cannot do this thing I cannot proceed to do other things... I used near infrared LED...


Code: [Select]
int voltage;
void setup()

           {
           pinMode(5, INPUT);        
           pinMode (6, INPUT);        
           pinMode (9, INPUT);        
           pinMode (11, INPUT);        
           pinMode (10, INPUT);  
         }
 

void loop()

    
          {
        
            analogWrite (5,200);
            delay(1000);
            analogWrite(5, 0);
            analogWrite(6, 200);  
            delay(1000);
            analogWrite(6, 0);
            analogWrite(9, 200);
            delay(1000);
            analogWrite(9, 0);
            analogWrite(10, 200);
           delay(1000);
            analogWrite(10, 0);
            analogWrite(11, 200);  
            delay(1000);
            analogWrite(11, 0);
            
          }

PaulRB

#1
May 25, 2016, 08:14 am Last Edit: May 25, 2016, 08:19 am by PaulRB
Please edit your post above and put in code tags so your sketch looks like
Code: [Select]
this

Have you tried the circuit on a real Uno?

Paul


Jack-kay

I'm not yet trying it... I got some advice to used 4051 multiplexer. thank you for your advice.

septillion

#3
May 26, 2016, 08:49 am Last Edit: May 27, 2016, 12:27 pm by septillion
What on earth has a 4051 to do with this?

Btw, the RC filter will not work with a LED because it loads the filter.

And even if it would it would really mess with the dimming. LED's are terrible at voltage dimming, especially with just a 47Ohm resistor.

Code: [Select]

           {
           pinMode(5, INPUT);       
           pinMode (6, INPUT);       
           pinMode (9, INPUT);       
           pinMode (11, INPUT);       
           pinMode (10, INPUT); 
         

But you try to drive a LED with those pins... Maybe make them OUTPUT?


So to recap, like every element of the setup is wrong... So just some advise:
- Drop the simulation. Nothing better then the real deal
- Drop the 4051, no application for it here
- Drop the RC filters, they do nothing
- Just connect the LED to a pin with a resistor
- If you don't want full brightness use a higher value resistor
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Paul__B


Jack-kay

I'm sorry actually I used Near Infrared (NIR) LED in this project
the method I used is NIR LED as a light source and OPT101 as the detector.
for your information, I already try to use the method that you explained.  the result is the LED is light up. but the problem is the sensor show the maximum reading.
when I try to use maximum PWM it is shown that the sensor also gives inaccurate reading.  after that, I try to lower the PWM reading that is 200 and 100. the PWM give  a relevant reading.
therefore, I used the PWM pin. the problem when using PWM pin is the voltage is unstable and I used RC filter to stable the voltage from PWM.
 the used of 4051 multiplexers is to control the LED to light up one by one.
I'm still learning... correct me if I make a mistake...

Wawa

I'm sorry actually I used Near Infrared (NIR) LED in this project

the method I used is NIR LED as a light source and OPT101 as the detector.

for your information, I already try to use the method that you explained.  the result is the LED is light up. but the problem is the sensor show the maximum reading.
 
when I try to use maximum PWM it is shown that the sensor also gives inaccurate reading.  after that, I try to lower the PWM reading that is 200 and 100. the PWM give  a relevant reading.

therefore, I used the PWM pin. the problem when using PWM pin is the voltage is unstable and I used RC filter to stable the voltage from PWM.

 the used of 4051 multiplexers is to control the LED to light up one by one.
I'm still learning... correct me if I make a mistake...
1) What project. You didn't tell us what you want to do.

2) A link to the parts would be nice. I had to Google "OPT101" myself.
This one?
http://www.ti.com/general/docs/lit/getliterature.tsp?genericPartNumber=opt101&fileType=pdf

3) How are you reading it. What conditions, setup, etc. What is "maximum".

4) With PWM on the IR LED I expect you get a PWM square wave from the sensor, if you are in the dynamic range of the sensor.
I guess sensitiviy can be adjusted with an external Rf.

5) if you want the supply the LED with a stable voltage, don't use PWM.
If you want to control LED power, you could use several outputs with resistors, and enable one or more pins as output.
But it might be better to adjust sensor sensitivity.

Why a multiplexer for the  IR LEDs. Did you run out of Arduino pins?
Leo..


Jack-kay

1) What project. You didn't tell us what you want to do.

Quote
2) A link to the parts would be nice. I had to Google "OPT101" myself.
This one?
http://www.ti.com/general/docs/lit/getliterature.tsp?genericPartNumber=opt101&fileType=pdf

3) How are you reading it. What conditions, setup, etc. What is "maximum".

4) With PWM on the IR LED I expect you get a PWM square wave from the sensor, if you are in the dynamic range of the sensor.
I guess sensitiviy can be adjusted with an external Rf.

5) if you want the supply the LED with a stable voltage, don't use PWM.
If you want to control LED power, you could use several outputs with resistors, and enable one or more pins as output.
But it might be better to adjust sensor sensitivity.

Why a multiplexer for the  IR LEDs. Did you run out of Arduino pins?
Leo..
1. my project is about to calculate the brix of fruit. I used 5 different led wavelengths as a light source. the output from the sensor will be trained as a model with a refractometer.

2. I'm sorry I do not give you the information... the link is correct.

3. I just used the general information.. that means I just take out the reading that Arduino read... for example the brighter the led, the high the reading of the sensor. when there is no light, it needs to show smaller reading from the sensor. but in my problem when there is light or no light, the reading is maximum...

4. yes it is correct that I get a square wave, therefore, I used RC filter to stable the voltage from the PWM pin... the voltage output can be control by the resistor, capacitor and voltage in...
I used this link to find the suitable voltage output to reduce some noise.
http://sim.okawa-denshi.jp/en/PWMtool.php

5. thank you for your advice.  I just need the sensor to be in an accurate reading. no matter the method. I need the LED to be brightest.

Wawa

I just take out the reading that Arduino read... for example the brighter the led, the high the reading of the sensor. when there is no light, it needs to show smaller reading from the sensor. but in my problem when there is light or no light, the reading is maximum...
Well, then start with the sensor, not the LEDs.
How did you connect the sensor to the Arduino.
Is there any ambient light falling on the sensor.
The sensor should output (close to) 0volt without any form of light.
Do you already have any code to read that.

Maximum output voltage = clipping.
Too much light... or too much sensor gain (R feedback).
Drop the idea of PWM-ing the IR LEDs.
Leo..

P.S. I know nothing about brix meters.

Jack-kay

Well, then start with the sensor, not the LEDs.
How did you connect the sensor to the Arduino.
Is there any ambient light falling on the sensor.
The sensor should output (close to) 0volt without any form of light.
Do you already have any code to read that.

Maximum output voltage = clipping.
Too much light... or too much sensor gain (R feedback).
Drop the idea of PWM-ing the IR LEDs.
Leo..

P.S. I know nothing about brix meters.

Brix is also called as the refractive index it is measured the total soluble content to express juice of fruits.

i directly used as shown in the data sheet.
pin 1 sensor connects to 5v and capacitor
pin 3 sensor connects to ground
pin 4 & 5 sensor connect to analog in A2
pin 8 sensor connect to ground

i try the sensor on breadboard there is some light...
in the coding i used 5 pin pwm and the general pin..


Code: [Select]

int a;
int voltage_A2 = A2;
int actualvoltage;
int voltage;
void setup()

           {
 
           pinMode(8, OUTPUT);          //LED 1
           pinMode (9, OUTPUT);         //LED 2
           pinMode (10, OUTPUT);         //LED 3
           pinMode (11, OUTPUT);         //LED 4
           pinMode (12, OUTPUT);         //LED 5
           pinMode (A2, INPUT);           //DETECTOR
 
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
 
        while (! Serial); // Wait untilSerial is ready
        Serial.println("Enter A To Start Take Reading");
        Serial.println();}

void loop()
{
   if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 'A')
     for (a=0; a<5; a++)
          {
       
            digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
            delay(1000);              // wait for a second
            voltage = analogRead (A2);
            Serial.print("LED1 ::   ");
            Serial.print (voltage);
            Serial.print ("\t");
            digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
     
       
            analogWrite (9, 255); //turn the LED on (voltage is High)
            delay(1000);  // wait for a second
            voltage = analogRead (A2); 
            Serial.print("LED2 ::  ");
            Serial.print (voltage);
            Serial.print ("\t");
            analogWrite(9, 0);    // turn the LED off by making the voltage LOW
   
       
            analogWrite (10, 255); // TURN THE LED ON (VOLTAGE IS HIGH)
            delay(1000);   // wait for a second
            voltage = analogRead (A2);
            Serial.print("LED3 ::  ");
            Serial.print (voltage);
            Serial.print ("\t");
            analogWrite(10, 0);    // turn the LED off by making the voltage LOW
     
         
   
            analogWrite (11, 255); // TURN THE LED ON (VOLTAGE IS HIGH)
            delay(1000);   // wait for a second
            voltage = analogRead (A2);
            Serial.print("LED4 ::  ");
            Serial.print (voltage);
            Serial.print ("\t");
            analogWrite(11, 0);    // turn the LED off by making the voltage LOW
       
       
 
            digitalWrite (12, HIGH); // TURN THE LED ON (VOLTAGE IS HIGH)
            delay(1000);   // wait for a second
            voltage = analogRead (A2);
            Serial.print("LED5 ::  ");
            Serial.print (voltage);
            Serial.print ("\t");
            digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
            Serial.println();
         //   return;
          //}
          }
          }
       
      else {
         
         }
}

Wawa

I have no idea of the mechanics of the brix sensor, but I expect that the sensor chip is way to sensitive with just the internal 1Megohm feedback resistor. If the sensor is too sensitive, the output will be pegged agains it's supply rail.

What happens if you just read the analogue pin with this simple sketch.
What are the average values with only ambient light.
And what are the values with the IR LED/current limiting resistor connected between +5volt and ground, and illuminating the sensor.
Leo..
Code: [Select]
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A2)); // the sensor pin
  delay(500);
}

Grumpy_Mike

Just a point. I wonder if it is relevant that pins 5 & 6 do not work and those are the pins that operate at a different frequency.

Quote
The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.
From https://www.arduino.cc/en/Reference/AnalogWrite

septillion

Besides all the above points (and they are very good points), why do you need to a very the amount of light? Don't you want a fixed level of light (which you can turn on/off).

PWM will blink the light rapidly so that might not work with the sensor. But voltage control of a LED isn't going to be very precise and just a RC filter isn't going to work because you load the filter.

You can try to make a variable current source to drive the leds.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up