1. 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.
    Dismiss Notice

Using PWM on a 18F13K22

Discussion in 'Microcontrollers' started by MrDEB, Nov 27, 2013.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I read Ghostmans post.
    OBJECTIVE is to simulate a candle
    PWM might be desired as looking at a candle, it tends to go slightly dim then bright. IMO I call this a flicker. The timer code posted after I changed the post scale seems ok but contemplating a 1 second "burst" of color to simulate the slight flicker if the PWM doesn't work. One issue with the PWM is it might require a mosfet thus increasing cost. COST is a major consideration.
    USE a RGB common ANODE led. KEEP current below 20ma (15-18ma for battery considerations) I have 200 of these on hand
    Use a 18F13K22 as it is low in price and available in an soic pkg.
    Need to control 3 RGB leds per micro (candles (LEDs) are placed at 2 foot intervals connected with cat5 cable).
    One battery pack supplying 4.5v
    Rechargeable would be nice but cost and project to be used once per year.
    At present dissecting the timer code to see what does what and how to change for desired effect if possible. Inserting an LED code snippet to indicate where and what the code is doing (code flow)
     
    • Like Like x 1
  2. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Project to run 4 - 6 hours. Using AA batteries estimated to run 14 hours
    using C batteries = 42 hours Actual run times are approximate
    LEDs to have hot melt glue drizzled on to simulate a candle flame
    project to be housed in a 3" section of white PVC pipe with two RJ45 sockets placed 180 degrees from each other for the two out board candles with same enclosure but no pic circuitry, just the RGB led
    I have poured over all these datails last week when I decided to look into this project.
    actual battery drain to be measured after I construct actual circuit using the 18F13K22 which I received in the mail this week.
     
  3. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    that was the kind of info i was looking for :D i have been told that my posts on this thread come accross as disrespectful!! i am utterly horrified that they can be read that way!! i am truly very very sorry if anyone has read my posts and taken them to be disrespectful! not once did it enter my head to show disrespect :( i did try and get a serious topic over in a lighthearted way. but i was unaware that the content could be read in a way that was never intended! so once again i am really sorry if i offended you or anyone else reading this. my mum and dad would be mortified if i showed disrespect. i feel i have let them and myself down, i would like to help but i dont think i am ready to help others yet and maybe i still need time to mature what i write so it's meaning is clearly understood. i wish you luck and will throw in the odd link if i think it can help, once again sorry everyone
    Loagn
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23

    I never felt you showed disrespect. Quite the opposite. You are very helpful. Somebody has wrong impression of you input.
    Now back to this candle. I redrew my schematic and used a different approach to metho tap 28 setup1.png d of posting image. I am making some progress after I changed the post scale value. The LED does not flash like a strobe light now.
    Am inserting at different locations in the posted timer code an LED routine but have yet to figure out why the led fails to come on as it should. Might be as simple as setting up the led routine correctly (discovered I was backwards to enable the LED)
    here is a different image format that was suggested eons ago using print preview in DIPTRACE
    This is my present test circuit
     
  6. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    is that dip trace? ive never seen it before but it reproduces awful on my screen! unreadable and blurry,
    unfortunately i use proteus because i am lucky to have access to it, have you tried eagle? it seems the top choice on here, i have tried it (free version) but that was ages ago and i just could get my head around the libs!!. maybe try LTspice? its free :D dont try and sim stuff tho because thats pretty hard to start with, but might do a schematic for you, are good old paint!! just do a box for the micro and put pin numbers on the wires (lines)

    it is easier to see now tho without the bus thing!! i have only ever used the bus thing a couple of times when i did a schematic with 2 lcd's in and i used it for the lcd dataline, otherwise i think they are horrible,
    i try and do my drawings in a logical way, i have no idea what the actual convention is and i doubt it matters much as long as there is some logic to it, for me that means when i do mine i try and make all VCC connections at the top and all GND at the bottom, i have all input stuff on the left and all output stuff here possible on the right, its kinda like writing a book!
    i spent hours and hours reading posts on here and looking at the drawings! then i tried to decide wich bits gave the most information and looked the best, so if you were ever to look back at my posts like i have done you can see my schematic style develop!! it was absolutely awful in the beginning! i look at the early ones now and cringe :D but slowly i think i have developed kind of my own style, but i do a schematic (normally several) for all my projects and i print them out to keep in a project folder along with the project notes and stuff! so slowly over time i started doing drawings the way that gave me the most info in the best way, but it's also got a bit out of hand as i am a bit of a neat freak like mum! so i have to have everything line up and lines the same length lol, i spend way too much time making it tidy but if i dont it drives me mad!
    did you look at the video? do a search on you tube for led candle and find the one you like most! that will be a good starting point! from there its easier to tell where you are with the code, a candle can mean different things to different people, its a really hard project to describe to someone else, what you call flickering i call something else so it gets to the point no one knows who is thinking what and it goes to rat poo!
    so pick a vid that gives you the closest look to what you want, from there its easier to work out how the code needs to be changed, at the moment i dont think any two people woud have the same pic in mind of what it should look like so pick a vid then everybody is on same page.
    and the apology wasnt just to you it was too anyone that thought i was being disrespectful, ii have re read it and i can see how it can be taken wrong, i was lucky in that you didnt take it wrong but as intended however all posts should be written so NO ONE is caused offense or is able to think the person is being rude! i am not being brought up to be rude.
     
  7. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    i have to get my project sorted tomorrow, but if i get chance (and access to dads laptop) i will redraw the schematic on proteus so it displays properly on here, i have to focus mainly on my project but will help where i can. like i said before go on you tube search candle led there is loads of vids!! take a look at them, remember all yout looking for is one you think looks best! dont worry about how they did it and dont bother reading there blabber about the project, what your after is the LOOK. then post it on here, that way we have the start point and everyone knows what you are trying to get too.
    did you buy all the chips in one go? or put another way are we stuck with that chip? dosnt matter if we are it has loads of good stuff like 4 timers! just means a slightly different approach. the 18f1330 would have been perfect but no idea what they cost. still at least you get a go at timer pwm!
    i have watched a few vids today and some look really good some look awful! you might have to start the code from scratch, but shouldn't be too bad, i think best way with the code when we get that far is to do it on here in pseudo code then you can take that and convert to SF.
    i will try and find the glue thing in a vid so you can see what i mean about it. i wouldnt use a straight led they are not great at that sort of thing they did a bit of diffusion unless you do stars with white leds (i did a huge one for my old bedroom! ended up with over 1400 leds in it :D
     
  8. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I decided to take a second look at what the code is ACTUALLY doing so I connected 3 RGB leds to the outputs then each color can be seen separately. I discovered what each variable is actually doing then proceed from there. At present trying to insert a BLIP as we will call it (a burst of one color to simulate a short flicker but can't seem to get the code to actually run the simple BLIP routine. Will post code after I figure out what is wrong. Contemplating instituting the software uart and see what the variables are.
     
  9. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    before you get too far would you mind posting a link to a video of a led candle so i can see what you are aiming for? also might be good to post a vid of what yours is doing, i dont have same chip as you but looking for one that is close enough. are you using free SF or the paid one? if its free one i can follow and maybe help that way.
    when you sit and watch a candle for real (i did for about an hour lol) they are not as random as i would have thought, once they settle is a very subtle change over a pretty wide range of different colours in the same group, for example they produce a huge range of yellows! i found it interesting in a geeky get a life kind of way :D
    the whole now i know what each variable does hammers home how important commenting code is, i have same problem with some i have that i did ages ago, its taken a long time to unraval it again and find out what i was wanting it todo! always seems like a waste of time when you first do the code but boy does it save agro later!
     
    • Like Like x 1
  10. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    ok ive been thinking about this.you could have this tucked away pretty quick, i dont want to offend but the following is a honest assessment of the project and also i will outline what i think would be the best and easiest way to do this . i know i am only a kid but in our house we are told to be honest so if my mum asks me if her bum looks big in this and it does then i tell her,

    1. at present you have no end point! this is a major one, no one can offer help with code untill they know what you want it to do, as it stands all that is known is you require a candle effect. I see that as too broad, its too open to interpretation, so as i suggested before the best solution is to find a video or more than one that explains in a visual way what look you want, we do a thing in school as part of the life skills class, its called communication enhancement something or other, to cut the lesson down to the basics what we had to do the other week was pair up, then what you do is sit across the table from each other, one person draws a basic house of there choice, then just using description you have to get the other person to draw exactly the same house! without seeing yours, you have 20 mins limit then you switch places! man it's really hard! but what it showed me was how difficult it is to describe something and have someone else see in there mind what you see, for help with this project i cant see any way to help with code until what is required is known, the most simple way is i think go find some vids and say i want this crossed with this or whatever. that way you have a end point.

    2. you are starting in the middle! if it was my project i tend to try and tackle mine in a set way. to make it easier for me i have to break everything down to small chunks and build up from there or i get lost in a mess of tangled code. so if this was my project what i would do after finding my end point, is look to see what colours i need, so yours are mainly yellows so start with yellow. then i would just concentrate on finding the duty cycles one by one that gave me the colours i wanted and as i found each colour i would write down the duty cycle of each channel so i could get that colour again, also you need to find the start and stop points of what duty cycles make yellows, that way you dont code a random colour like purple say into it, then move on to say oranges and do the same.
    repeat above steps untill you have a list of codes that give you all the colours you want or at least get a list that will give you each band of colour like yellow,orange,funny red kinda blue colour etc.
    once you have this you have your colour table for the array

    3. next i would work on pwm for making a led dim to bright and play around with values just to get the variation in intensity, again write down the values that give you the looks you like, this is then the figures for the intensity array, as with the colours you can always random the numbers as long as they are within your set limits this will give a more natural look

    4. next bit simples!!! just throw in a range of different complete off cycles of various lenghs. again experiment but probally look in the range of 500ms to 980ms or there abouts!


    5. go have a cup of tea or coke in my case sit back relax

    6. put it all together and bingo one kick bottom candle :D:D
     
    • Like Like x 1
  11. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I need to locate my computer cam laying around here somewhere.
    This morning I did a fresh look at my code and connected 3 RGB leds (one color for each output).
    I discovered what variable changes amount of color intensity. Need more time to tweek but very close right now. I have a slightly modulating yellow LED that looks pretty good but trying to introduce a BLIP of color to produce a random flicker for say 1 second.
     
  12. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    i only did a quick test but i think you might find 1 second too long!! i wouldnt be surprised if you find the sweet spot around 500ms -750ms! but your the man on the ground lol so its your judgment call :D, also i found in my quick test that different rgb leds from same batch gave different colours (slightly) for same code!!! i also thought about putting a different resistor on each colour channel so they are slightly different intensity right from the off! but impossible to say sat here when the led is all the way over there!! having trouble downloading sword fish or i would try and follow. also i have a crisis here! i have totally mistimed my hatch! because i am trying a commercial incubator for the first batch then building mine from the knowledge of what i liked about the commercial one and what i didnt (isnt much i do like about the controls on this one! for a £1000 incubator its crap!!!)
    but anyway the idea was too see what i thought was good and what i thought needed doing differently! somehow i miscalculated the hatch date and thought i had 4 more days to get the brooder ready! GUESS WHAT, they are hatching right now!!!!!:eek::eek::eek::eek::eek:, lucky for me dad has stepped in (good old dad) and is sorting them out with a temporary home! but means i need to stop the bit of the project i was on and jump to the end bit and sort controller for the brooder (fairly easy is just temp monitor and control if too hot/cold) i have some rubbish I2C temp sensors i can use (2C accuracy!!!) but for what i have in mind its good enough for this bit. i will post pics later if i get time or tomorrow!
     
  13. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Well I got the webcam working and here are the present results

    I think it is ok maybe cut down on the blue some more for a better amber color?
     
  14. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    Tweeked the second program code using CONST array. Tried inserting a random number generator but it didn't seem to operate correctly. Had none of the bits for porta.1 enabled but it still came on?
    youtube video
    Code (text):
    {
    *****************************************************************************
    *  Name    : UNTITLED.BAS                                                  *
    *  Author  : [select VIEW...EDITOR OPTIONS]                                *
    *  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS]              *
    *          : All Rights Reserved                                            *
    *  Date    : 11/30/2013                                                    *
    *  Version : 1.0                                                            *
    *  Notes  : candle code that works but very simplistic                                                              *
    *          :                                                                *
    *****************************************************************************
    }
    Device = 18F2420
    Clock = 8

    Include "InternalOscillator.bas"
    Include "RandGen.bas"
    Include "Utils.bas"
      Const led(12)As Byte = (%11111000, %11111010, %11111010,%11111010,
                              %11111010, %11111000, %11111010,%11111010,
                              %11111010, %11111010, %11111010,%11111010)'first 3 bits G B R)
    Dim INDEX As Byte
    Dim x As Byte
    Dim LED1 As PORTA
    Dim RandomVAL As Byte
      Sub flicker()
      Dim x As Byte
          For x = 0 To 12
          ' RandomVAL = RandGen.Rand
            'PORTA = led(RandomVAL)
          PORTA = led(x)
          DelaymS(300)
          porta = %11111010
          delayus(700)
            Next
        End Sub
        ' START OF PROGRAM
      INDEX = 0
    TRISA = %00000000  
    SetAllDigital
    RandGen.Initialize(12)
        While true
      ' RandomVAL = RandGen.Rand
              flicker
           
     
        Wend
     
  15. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,197
    Likes:
    109
    Location:
    Seattle, WA
    ONLINE
    Your results do look exactly like a candle! Wow. A candle burning in an atmosphere containing random pockets of methane gas. Depending on the crowd at your event, this may be exactly the effect you are looking for.

    If not, maybe it's time to take a look at the RGB color chart I linked above. The area circled in blue might be the limited range of colors found in a candle.

    So what does the text in each box mean? It's the RGB color in hex. For example, the block in the middle of the orange section is ffd75f. So $FF is the hex value of the red on an 8-bit scale – $FF = 255 in base 10 = 100% on. $D7 is the hex value of the green = 215. The green is on about 84%. $5F is the hex value of the blue – you can figure out what percentage this is.

    If you limit "random" values to acceptable color values from this chart, you might achieve the look of a candle on earth.

    image.jpg
     
    • Like Like x 1
  16. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    great chart! where did you find that. very very handy!
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You all doing a great job send it the pin combos and kill the power every now and then it may look like the real thing. Oh and some hot glue to bring the color up to a point. Helps
     
  18. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    the white hot glue works really well, you put a blob on the led then with a bit of stiffish wire keep swirling it round the led as it cools then right at the end bring it to a point! can take practice but works well. i use hot glue alot with leds to get different affects, is great for diffusion.
    you also need a bit more dimming (pwm the anode) vary the dimness but dont go too bright it makes it look like a led being used as a candle
     
  19. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I realize it isn't perfect but the second code with the const arrays achieves exactly the same results as the long code using DUTYVAL etc. AND I wrote the code from scratch.
    Now on the hot melt glue, yes I plan to apply to the LED but I get the impression there are clear and white? Te white would perhaps be best.
    As far as the chart, nice chart, thanks and after attending church this evening I paid extra attention to the candles with real flame. Going to put more resistance on the blue cathode. I want to retain as much brightness as feasible since people are going to be eating to the "candle light".
    Still need to locate a C battery holder for 3 C batteries but in a triangular form if there is even one out there. Fond one for AA but feel the battery drain of three RGB leds ( 20ma per x 9 = 180 ma max ) so C batteries may be over kill but just to be safe. Yes I know each RGB led will be drawing less than 60ma per but plan for some head room.
     
  20. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,197
    Likes:
    109
    Location:
    Seattle, WA
    ONLINE
    I think you don't understand the color chart or how to get ANY color from an RGB LED.

    In order to make red for example, you illuminate the red element at 100%, and the green and blue at 0%. To make an orange color as I outlined above, the red LED is on at 100%, the green at 84% and the blue at 37%. This comes from the chart as I've explained above.

    So how do you get various percentages of "on"? This is where PWM comes in. Adjust the current limiting resistors so that all three elements draw the same current, calculating this from the forward voltage at the desired current for each element. They WILL NOT all be the same. Then vary the PWM duty cycle of each element to the percentages shown on the table. Depending on how well the LED sections are matched, you'll be close to the expected color.

    Oh snap. RGB means three elements to control and only two PWM modules are available. Now what? Look at the table above. All of the reasonable candle colors start with FF - the red element is on full across most of the possible range. The variations come from changing the amount of green and blue added to the mix. So connect the red element to a standard port pin, and the green and blue to PWM outputs. Now you can vary the color across the range.

    This doesn't address putting 3 candles in parallel. Not a very realistic effect to have all three vary together. You have another problem anyway. A PIC18F-series port pin can only source or sink 25 mA.
     
  21. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,737
    Likes:
    433
    Location:
    SCOTLAND
    i think the original drawing had a mosfet, and oops i mentioned a resistor the idea being to make sure no two candles had same brightness, but to be fair i had no idea food was going to be eaten by candle light:facepalm:
    i thought it was just a decoration!
    go with clear hot glue then as the white stuff cuts the light a fair bit unless you used to messing with it.
    i dont do basic but i am going to have a try and work out your code, as i am unsure what it is doing, it really would make your life easier if you had planed the effects first! that way you could have made sure each led was different, one way would have been to pick a frequency then set a timer to roll over every 100th or 200th depending on frequency the amount of resolution may differ but for leds a lowish frequency would have given plenty, anyway each time timer rolls over you do a look up and say you want a green connected to port B2 off you just switch off at that point, so for 50% switch of after timer rolls 50 times (you keep a counter i should have mentioned that) have a look at my thread about transistor switching it talks about fans and pommie did a post explaining simple timer pwm really well.
     

Share This Page