Writing code for battery operated projects


Closed Thread
Results 1 to 16 of 16
  1. #1
    jessey's Avatar
    jessey Guest

    Default Writing code for battery operated projects

    Hi, This is my first post here....... hello everyone. I'm using a 16f877 non A micro and am trying to write efficient code for battery operation. Could anyone suggest any considerations to do this? I'm using the latest version of PBP and Micro Code Studio. The micro is consuming .7 to .8 milli-amps in a sleep loop that looks for a button press. I thought I should be able to get it down much lower. I checked the archives and read many discussions but couldn't find any help that specifically related to my code. It was helpful for some aspects but not all. I have code written that uses an ACD and PWM code to sound a buzzer, also I am using the pot command that reads a photo sensor that directs the code to go into a sleep loop when its dark.

    For the ADC, I'm turning it off using ADCON1 = 7 then I set the adc pin to an output then make the output low and to turn it back on I'm using TRISA.0 = 1 to make it an input and ADCON1 = 4 to turn it back on. I'm not using the ADCin command to check my adc input. I'm not sure if this is the proper way to do this or not?

    What about when using PWM and Pot commands? Is there anything I should be aware of using this code? There's something consuming current but I can't figure what it is, it could be my adc. Also, just to check for consumption I put the Micro to sleep with a "SLEEP 30 command (no loop) and the milli-amp meter fluctuates form .7 to .9 milli-amps, shouldn't the reading be steady while in sleep with no other code being read? Any help would be greatly appreciated.

    Thanks jessey

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Hi Jessey

    1. Turn OFF all unused Hardware in your PIC befor going to SLEEP... ie... switch-OFF the ADC, PWM etc. That includes switching-OFF your weak pull-up's too.

    2. Is this a NIGHT/DAY Sensor type application or a Room Occupancy Sensor kind of application? If it's NIGHT/DAY, then it doesn't get light instantaneously... so why not go to SLEEP for longer periods of time - like ten Seconds or so.

    3. If you use Comparators (eg 877A) rather than an ADC, you can have your PIC woken from SLEEP when the light level exceeds a pre-set value.

    4. There are better PICs than the 877 for Low-Power use... Microchip has a whole bunch of micropower devices that only consume a few uA when running - let alone when asleep. Also, that's a BIG PIC (in more ways than one!)... so I'm questioning whether you need all those pins for a simple Battery powered application. The 12F683 for example has Comparators, ADC and PWM and will consume far less power than that old 877 (quite apart from the fact that the 877 is probably bigger than your average car Battery!!!). Actually, I've got 877's gathering dust in a drawer here for over two years because it's just not worth designing products with that device.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    in addition to Melanie, turn off all power that goes to your external device..pot and blah blah. Also, use voltage regulator with the lowest quiescent current need. Regular 7805 are not appropriate. 78L05 are better but there's a bunch of more efficient.

    12F683 is a good choice if you need PWM and ADC. The POT command is not appropriate to read from a POT when your PIC have internal ADC. Should use ADCIN instead. 12f675 will also do the job if your skip the PWM to a home-made sound procedure.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Melanie & Steve

    When I said I wasn't using the ADCin command I should have elaborated to say that I am using the adc by setting ADCON0 = $41 then starting the conversion with ADCON0.2 = 1 instead of just using the ADCin command.

    My project is a moisture meter circuit that uses two stainless steel wire sensors that are inserted into the soil of a potted house plant. I have an external CMOS circuit that outputs an analog voltage that is proportional to the wetness or dryness of the soil which is connected to the adc input pin. This moisture value is printed to the top line of a 2 X 16 Lcd, the bottom line shows an Alarm Set value that is user adjustable. When the plant is just watered then the Moisture will show a value in the upper range of 255 to the top line of the Lcd and as the plant dries out then this value will gradually decrease. When the user decides that the plant is dry enough to require a watering then he or she programme's the Alarm Set # on the bottom line of the Lcd to match the Moisture # and when the values match then the alarm sounds. Then when the plant is watered, that silences the alarm, until the next time the plant dries out again to that set value then the alarm sounds again. So this is used as a reminder as to when to water your plant. I was thinking of calling it the Water Minder. I think it would be cool to use a digital voice chip to announce when the plant needs a watering but for now the piezo buzzer works good. Would anyone know which voice chip would be a good choice for a battery operated project like this one?

    I have the circuit wired so that it will operate on either the mains or batteries. The batteries +6V output is wired to the normally closed contact of a relay. The common input of the relay supplies the + voltage to the micro and everything else. A TX, when plugged into the mains turns on the relay which disconnects the batteries and then operates off the TX via a voltage regulator circuit which is connected to the normally open contacts of the relay. I am thinking of using 4 AA alkaline batteries with no voltage regulation. Would that be the best way to go?

    When operating off the mains then a pin is set low on the micro to disable the auto off of the Lcd and any code related to the sleep loop mode. When operating off the batteries then a counter is used to automatically shut off the Lcd after 1 minute and then places the micro into a sleep loop that monitors a push button that turns back on the Lcd. When the sleep loop times out then a photo sensor is sampled and if it's dark enough (the user programme's what level is considered dark) then it jumps back to the sleep loop, if it is light enough then the CMOS circuit is turned on via a pic pin. Then the ADC is turned on and the ADC is sampled and if the plant is dry enough (if not then back to sleep & shut off everything again) then a miniature motion detector is turned on via another pic pin and the alarm sounds with a succession of single beeps followed by multiple beeps grouped together which lasts a total of 6 minutes. If the user didn't hear the beeps after 6 minutes then another loop is entered that monitors the motion detector for an activation & also the Lcd on push button and it just sits there waiting, when the loop times out then the photo sensor is polled again and if its dark then it jumps back to the sleep loop otherwise it just sits there waiting for someone to come around so they can be alerted.

    I've been playing around with the method that I was using to turn on/off the Lcd and have it down to consuming .38 to .45 milliamps in the sleep loop and 3 milliamps when the Lcd is on, so its getting better. I thought it was my adc consuming current but now I know it was the Lcd too. Can anyone tell me what would be an expected milliamp draw or what I might be able to get it down to using the 16f877? I have my Lcd wired in a 4 bit mode to the 877 (7 pins used for the Lcd) and with 6 pushbuttons and external circuits needing pins to power them up, I think the 877 is a good choice and price ($6.79 CAD in lots of 10) as my code is up to almost 3000 words now and I want to also add directions to the Lcd that will explain all the functions of the device to the user which could easily use another 3000 words. Also can anyone tell me how to shut off the pot command if it consumes current when not being read? I am using TRISB.4 = 0 when I'm not using the pot command and then TRISB.4 = 1 just before I read it.

    Thanks jessey

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    still too much current for a battery perated system. Can you post your schematic so we can see which improvement can be done???
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Steve

    Here is the schematics, I finally got it finished. While I was drawing the schematics it occurred to me that I could get rid of the transistor that outputs to pin 23 by pulling the pin low (instead of high) with the resistor and then just tie the TX's +5V output directly to that pin. I have all the unused pins set as inputs, should I be tying all the unused pins to negative with the 10k resistors or would it make any difference?

    While I was trying different wiring schemes to turn on/off the lcd, I ended up frying my Lcd ...... oops! I was switching both the positive and negative for the Lcd off with transistors, I still can't figure out what I did wrong to fry the Lcd. I thought I'd wait to see what you could suggest as to how I have the Lcd wired before I start playing around with a new one and end up frying it too! There must be something wrong with the way I have the Lcd wired because even If I disconnect both the positive and negative for the lcd the milliamp draw is still .38 to .48 milliamps while the micro is in a sleep loop. I also noticed that when I disconnect the negative to the Lcd (pin 1) the Lcd still works good? When I disconnect all 10 wires to the Lcd then the milliamp draw drops to .007 to .122 ma which is much better, so I'm thinking that it must be wired incorrectly but I can't see what I've done wrong. Any help will be very much appreciated.

    Thanks jessey
    Attached Images Attached Images  

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    What i suggest, have an i/o wich will cut/apply power to all external devices. Use a transistor/mosfet/realy to do it. OR since i/o can drive/sink 25ma, you can use some of them to do the job as well. 1 for LCD(VDD and contrast line), 1 for CMOS circuit, Pull-up resistors and so on for all the other things. Before go to SLEEP, set all i/o to input. By doing that, all supply will be remove to external devices.

    Once you'll get out of SLEEP, just reset TRIS to normal state, add a little safety sake pause for the LCD start-up, Reset LCD flags FLAGS=0 and you're in business.

    Quote Originally Posted by jessey
    should I be tying all the unused pins to negative with the 10k resistors or would it make any difference?
    it's up to you.
    Quote Originally Posted by jessey
    I also noticed that when I disconnect the negative to the Lcd (pin 1) the Lcd still works good?
    that's possible, he take his ground from XYZ pins, contrast, data,r/w or else.

    Some suggestions
    • For the pin 23, you can also just use a resistor in serie with VCC
    • remove the relay and use only two diode. both cathode together as the VDD output, one anode to regulator, the other to the Battery. Cheaper, smaller and works!!!

    hope this help.

    Small reading from Microchip about power saving
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Steve

    Thanks a million!

    With your helpful suggestions I finally got the consumption down to .009 milliamp draw in my sleep loop. I had a few problems that I still don't understand. It is working good though but at first when I implemented you changes I was still getting .4 to .5 milliamp draw.

    Here is what I did:

    I am now using pin 20 to turn on the Lcd's VDD and the contrast's +5V by having it set as an output and forcing it high to turn them on, then to shut them off I did as you suggested and set the TRIS to an input for pin 20 and that works great for that part. I also did the same thing with pin 21 and connected the negative for both the Lcd and the contrast to that pin and that works great too. I'll use the same procedure for all the other external circuits later as well.

    I also moved the R/W of the Lcd to RE2 and added Low PORTE.2 before my goto mainloop statement. Then just before going to sleep I shut off the VDD and the contrast's +5V and the grounds then set all the TRIS for all the Lcd pins as inputs but was still getting .4 to .5 milliamp draw on my amp meter in sleep.

    I checked all the Lcd pins with a digital logic probe and found that the DB4, R/W, the Lcd's VDD and the +5V & -5V for the contrast were all high. When I disconnected DB4 then all those pins that were high went low and the milliamp draw started to fluctuating from .009 to about .107 milliamps which I was pretty excited to see!

    In my sleep loop, I'm monitoring a push button so I have the option of turning on the Lcd while in sleep and that's when the milliamp draw jumps to .107 but it's only there for a very few milliseconds. That's great, using a photo sensor to keep the program in the sleep loop when it's dark and using a motion detector to silence the buzzer when no one is around to hear it should make the batteries last for 6 months or so. That's great!! Would you have any suggestions on a voice chip that would good for battery operation?

    The problem was, that I was getting a high on pin 27, seems that setting the TRIS works on all the other Lcd pins on PORTD except for RD4 for some reason? I tried forcing RD4 low using PORTD.4 = 0 when I shut off the Lcd and it's working great now.

    Any ideas on what's happening there?

    Thanks jessey

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    for some voice module... winbond do some chip that allow you to record your own message. I'd use ISD4002 few times and they work great. Not cheap but easy to use and you don't need any external/third party programmer to use them. There's probably a tons of other but this is the only one i'd work with.

    I think they also do some Text-To-Speech module to. have a look to their Website
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Can anyone help me?

    Hello, My program is functioning good now for the better part, the milliamp draw is down considerably which is great. Thanks again Steve! Now I need a kick start to: figuring out a way to set up a variable (if it's at all possible?) that I can use in an IF...Then statement to sound an alarm when the battery voltage (that powers the pic) drops to the level that the user can set using push buttons to change that variable. I did hear that there are voltage regulators that have a low voltage alarm built in so that's an option too.

    For now I'm using a 5K pot connected to an ADC input in a 10 bit mode to simulate the batteries output voltage. Later I'll set up a voltage divider for the batteries and tap it off to the adc input. The code below that I'm using seems to be functioning as expected thanks to the PBP Archives.

    The_Battery_Voltage = (The_Battery_Voltage */ 500)>>2
    LCDOut $fe, 1,"DC Volts = ",DEC (The_Battery_Voltage/100),".", DEC2 _
    The_Battery_Voltage

    With the above code my 5K pot adjusts from 0.00 to 4.99 volts which as far as I can figure so far, should be good. I did a print to another screen without the math to show The_Battery_Voltage variable and it showed 1023 while the one with the math above was printing 4.99.

    Would anyone know the math involved to create a word variable to accomplish this? Another option I have is to use another adc input using a 5K pot and use that variable in my IF...Then but I would like to go with push buttons. Any help would be greatly appreciated.

    Thanks jessey

  11. #11
    Join Date
    Jan 2005
    Location
    Puerto Rico
    Posts
    133


    Did you find this post helpful? Yes | No

    Smile Volt Read

    KK:
    gosub GetVolt
    SerOut LCDScreen, N2400, [LCDCmd, LCDLine1,"T= c"," V=",#LCDVHigh,".",#LCDVLow]
    goto KK

    GetVolt:
    'For global reference to system voltage, use the Voltage variable, which is based on a
    'scale of 1 to 1000, with 775 = 7.75 volts. Remember that the resistors on the board
    'divide the actual battery voltage in half so the ADC can "digest" it without damage.
    'ADCIN voltages are limited to 5. Voltage are compared with the onboard
    'precision voltage reference which delivers a perfect 5V.


    '**Voltage**
    LCDVLow=0
    LCDVHigh=0
    Voltage=0
    ADCIN VoltChannel, Voltage 'Take just 1 voltage sample. 7.6V = 194
    Voltage = Voltage * 4 '194 * 4 = 776
    LCDVHigh = Voltage / 100 '776 / 100 rounds to 7
    LCDVLow = (Voltage // 100) / 10 '776 // 100 = 76, 76 / 10 = 7
    'Final Display = 7.7

    return
    Last edited by jetpr; - 13th June 2005 at 18:12.

  12. #12
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Question

    Just a thought !
    When using the ACIN command for A/D conversion
    How is this turned off before going to sleep ?
    Does pbp automatically take care of it


    Isaac

  13. #13
    Join Date
    Jan 2005
    Location
    Puerto Rico
    Posts
    133


    Did you find this post helpful? Yes | No

    Smile Volt Read

    yes Think automatically take care of it i dot have no problems using the A/D.

  14. #14
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jetpr
    yes Think automatically take care of it i dot have no problems using the A/D.
    Thanks thats what i thought

  15. #15
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Jetpr,

    Thanks for the code you sent, I got it working good and I also managed to write some code using push buttons so I can set a reference to warn when the battery voltage drops to whatever level I want, that way if it works the way I envision it to work then I can use different battery combinations and just set my reference (dependent of pretty much any battery source) to sound an alarm when the batteries voltage drop to a certain level. I managed to find some 1% 10K resistors at my local electronics store and with your code it shows 6.5 volts with 4 fresh AA alkaline batteries, the actual voltage of the batteries is 6.43 volts using an old Micronta DMM to check them but I think that's ok. I connected a 5k pot to the ADC as well and it Max's at 10.0 volts and down to 0.0 volts, I'm hoping that's ok? This is my first time working on a project that uses batteries so it should be interesting. I have the reference push buttons code set up so I can set it to tenths of a volt, just like the code you sent me. I can't wait to get some good voltage regulators to try it out.

    Thanks man..........

    Issac wrote:
    Just a thought !
    When using the ACIN command for A/D conversion
    How is this turned off before going to sleep ?
    Does pbp automatically take care of it

    Hi Issac,

    What I did is to use ADCON1 = 7 statement to turn off the ADC then I set the ADC pin as an output and then set it low with PORTA.0 = 0, that seemed to work good. I noticed that I saved on my milliamp draw by setting the ADC pin low after shutting it off. Before I tried setting it low, the consumption didn't go below (if I remember correctly) about .068mA and would fluctuate (bouncing up and down) up to about.086mA. Then after I set it low it dropped to about .006mA minimum and fluctuates up to about .044 -.060mA and about every 4 or 5 seconds it jumps up to .104mA but just for a ever so brief time, so it did make a big difference forcing it low. To turn the ADC back on I just used Trisa.0 = 1 then ADCON1 = $0E or ADCON1 = 2 but that'll depend on which chip your using..........good thing for me they keep Archives! LOL

    Maybe someone could tell me if that's the proper way of doing it, I would imagine there is more than one way of doing it for sure. I just ordered some MIC2954, 250mA Low Dropout Voltage Regulators. The web site I ordered them from just listed them as MIC2954, I don't know if their MIC2954-02BZ or MIC2954-0B3Z or even what the difference is, I think maybe I read something about .5% and 1% but their suppose to be pretty good for battery operated micro's. I also read you have to play around with the capacitor values to get the minimum amp draw. It looks like their pin compatible with a 7805 in a TO-92 case. It says in the data sheet that (if I'm reading it properly) that it uses a low quiescent current (120mA typical) and that it will operate down to 2 volts and produce a 5 volts output? I'm not very good at reading data sheets, anyone here used these before with any success?

    Thanks jessey

  16. #16
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jessey
    It says in the data sheet that (if I'm reading it properly) that it uses a low quiescent current (120mA typical)
    you probably read correct but paste with error...120uA quiescent... wich is more suitable

    i've never use those but they looks good.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Writing good/proper code
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 22nd July 2009, 00:55
  2. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  3. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  4. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  5. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts