Page 1 of 2

ADC & PWM example please

Posted: Sun Sep 09, 2012 8:38 pm
by acestu
Hi,

Could anybody post an example of ADC & PWM please, such as a potentiometer controlling the brightness of an LED, that would
be brilliant as a starter....

Thanks In Advance
Acestu

Re: ADC & PWM example please

Posted: Sun Sep 09, 2012 9:01 pm
by medelec35
can you give us more details please.
E'g what version of Flowcode are you using?
If its Flowcode 5 Demo then goto:
http://www.matrixmultimedia.com/lc_index.php?p=18
Download: Flowcode v5 Examples - All
Then within:
Components\PWM & Analogue Output Folder
there are examples using PWM component and bit banging using timer interrupt.

Martin

Re: ADC & PWM example please

Posted: Sun Sep 09, 2012 9:16 pm
by acestu
Hi Martin,

I have flowcode 4, at the moment and I am hoping to purchase a Flowcode 5 bundle on the 22nd of this month, I found a
pwm example on youtube but it isn't connected to anything, just a pot altering the brightness of an LED would be great.

Thanks
Acestu

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 10:32 am
by Enamul
Hi,
This program will change the brightness of LED depending on ADC value. I am using PIC16f877a and PWM1..so LED has to be connected to RC2.

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 5:33 pm
by acestu
Thanks Enamul,

I am going to have a look at this now.

Acestu

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 5:42 pm
by acestu
Hi Enamul,

When I run the simulation, the duty cycle changes when you alter the pot, but the LED does not light,

Am I doing somthing wrong ?

thanks
Stuart

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 5:59 pm
by Enamul
Hi Stuart,
It will run in hardware but doesn't simulate..take care about ADC channel as AN1 is used not AN0.

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 6:32 pm
by acestu
Thanks Enamul,

I was just wondering about the pot, I take it that you use a linear pot rotary or slider but what resistance ?

thanks
Acestu

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 6:51 pm
by Enamul
Hi,
In hardware, you can simply use any pot linear or non-linear..if you want to have smooth intensity variation you have to use linear pot. 10K ohm pot should be nice to use.

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 6:53 pm
by acestu
Thanks Enamul,

I will try this on the breadboard now...

Acestu

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 8:34 pm
by acestu
Hi Enamul,

I have put the circuit togethor on a breadboard, I had to use a 16f877 though because that is all I have,
when i powr it up though the brightness of the LED is controlled by the first few millimeteres on the pot
and is really difficult to control, the crystal is the same, have you any idea why this is so ?

thanks
Acestu
LEDPWM1.jpg
LEDPWM1.jpg (106.26 KiB) Viewed 13773 times

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 10:30 pm
by medelec35
Hi Acestu,
If you would like to post the flowchart you used, then that can be checked out to see if there is anything that's been overlooked.

Martin.

Re: ADC & PWM example please

Posted: Mon Sep 10, 2012 11:43 pm
by acestu
Hi Martin,

The flowchart is the one above that Enamul posted for me, all I have done is changed the pic to a 16F870.
ADC Controlled LED PWM 16F877A2.fcf
(6.5 KiB) Downloaded 488 times
thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 6:43 am
by medelec35
Hi Acestu,

I would suggest you do a 1Hz LED flash test so you can confirm hardware is running at the correct Speed.

E.g

Loop while 1
1>C1
delay 500ms
0>C1
delay 500ms

If that is ok then if you have a multimeter then measure voltage between pin 3 and GND. Make sure there is a full range (0 to 5V) as your turning pot.

Finally measure dc voltage across Pin 13 & GND. Meter should read average DC, so should vary from 0 to approx 5V.

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 10:12 am
by Enamul
Hi Stuart,
Sorry I can't reply immediately. I wasn't in desk. I think you want to say you are using PIC16f870 that you have not 16f877a. What is the value of crystal you are using on the bread board?

Can you test one simple think like if you turn the pot can you get the voltage in wiper from 0V to 5V? Another basic thing you might have done but just to become certain, did you put series current limiting resistor with LED?

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 5:56 pm
by acestu
Hi Martin, Enamul,

Firstly, it's ok Enamul, I don't expect you to respond straight away, i'm just glad that you answer my posts.

I will try what you said Martin, and yes Enamul I am using a resistor with the LED is this correct ?

p.s. my crystal is 19.---- as per flowcode setting

Thanks Guys

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 6:09 pm
by acestu
Hi again,

I have measured the voltage between the wiper (pin 3) and ground and I am getting 0 to 4.99 volts

the LED lights between 0 and 1.88 volts

How do I do the 1HZ test? , do I go into MPLAB and just write that little program or can I do it in Flowcode ?

thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 7:19 pm
by Enamul
I will try what you said Martin, and yes Enamul I am using a resistor with the LED is this correct ?
Yes..that's fine. You have to use resistor. That's why I was asking. I think you mean 19.66MHz which is ok.

The attached program is flasher LED program. If you see using the attached program LED ON and OFF every 1 sec..in that case your configuration setting is OK and crystal is OK and chip is OK. Just an easy way to test whether these basic stuffs are ok or not.

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 7:38 pm
by acestu
Hi Enamul,

When I program the chip with the 1HZ code the LED that I moved to B0 just stays on all the time ?

Any ideas ?

thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 8:13 pm
by Enamul
Hi,
Do you have any other chip to try? It might have that chip has problem..

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 8:18 pm
by acestu
Ok Enamul, I will have a look and see if I have another.

thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 8:30 pm
by acestu
Ok I found another chip and same thing happens,

Does it matter which side of the LED the resister goes ?

Should the LED be on all the time even when I Erase the program from chip memory


I must be doing something really silly somewhere !

thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 8:45 pm
by acestu
OMG, how stupid am I, I have just seen that connections used on the chip are white in the diagram, and there are 2 x vss connections

I only had 1 of them connected, when I connected the second one and re programmed LED flasher code it worked, so I am going to try
the other code now......


Thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 9:09 pm
by acestu
Guess what ?

It works, brilliant thanks guys,

could I just ask 2 more question though please,

Is this method used in domestic house light switch dimmers ?

When using PWM to control DC motors are they driven directly from the micro or does it have to be amplified somehow ?


Thanks
Acestu

Re: ADC & PWM example please

Posted: Tue Sep 11, 2012 9:24 pm
by Enamul
Is this method used in domestic house light switch dimmers ?
When using PWM to control DC motors are they driven directly from the micro or does it have to be amplified somehow ?
Actually most of the dimmer circuit used TRIAC to control power in the LOAD(light) which is triggered by RC circuit in normal household lights and FANs if that is run on AC. so you have to have zero crossing detector and triac control circuit to use this with AC light dimmer..
But for DC light control you can simply use TRIAC or SSR with transistor as driving circuit.