ADC & PWM example please

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: ADC & PWM example please

Post 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
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post 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.
Attachments
ADC Controlled LED PWM 16F877A.fcf
(6.5 KiB) Downloaded 781 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post by acestu »

Thanks Enamul,

I am going to have a look at this now.

Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post by Enamul »

Hi Stuart,
It will run in hardware but doesn't simulate..take care about ADC channel as AN1 is used not AN0.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post 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.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post by acestu »

Thanks Enamul,

I will try this on the breadboard now...

Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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 13591 times
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: ADC & PWM example please

Post 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.
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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 485 times
thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: ADC & PWM example please

Post 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.
Martin

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post 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?
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post 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.
Attachments
Flasher_LED.fcf
(4.5 KiB) Downloaded 349 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post by Enamul »

Hi,
Do you have any other chip to try? It might have that chip has problem..
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post by acestu »

Ok Enamul, I will have a look and see if I have another.

thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: ADC & PWM example please

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC & PWM example please

Post 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.
Enamul
University of Nottingham
enamul4mm@gmail.com

Post Reply