# Question regarding PIC24F's..

Joined Feb 9, 2012
122
Hi all.. So in surveying the available microcontrollers and RTC timers I need to have at my disposal, I decided to move away from the MSP430 + external RTC (DS1337) in favor of the PICF24 series that has the built-in RTCC.. That allows me to keep down the cost and alleviate an external part and it's interfacing.. As someone that has never programmed a PIC but has programmed various other embedded platforms (Arduino, PPC,etc), I wanted to get your input on whether the processor I plan to choose has enough memory to do what I want.. The specific part I'm looking at is the PIC24F16KA102 which has 16k program memory + 1.5Kb SRAM + 512b EEPROM and has two I/O ports (16-bit Port A, 8-bit Port B) which I gather can be used to drive displays,etc

In my project, I'll have an LCD display of some sort that will hopefully be connected via SPI and a few buttons. The controller will allow the user to set the current time and set some wake-up points to do some "work" which in this case is to trigger a relay for running a small motor for <30 seconds at which time it goes to sleep. My initial hunch is that the generated code for this sort of small application ought to easily fit in the 16Kb program space but I thought I'd ask you all first as you've likely got more experience with these PIC processors than I do.

This is one area that I had issues with on the Arduino -- it was fairly easy to run out of space -- particularly for a data logger that wrote (and had code for a FAT32 library) to an SD card... In this case, no logging needed so hopefully the needed space in the program area will be low.. ?

#### ErnieM

Joined Apr 24, 2011
8,183
So this is basically a 30 second timer and clock? Is that an alphanumeric display or graphic? If so, 16K should be plenty, especially with an internal RTC.

Will the power typically always be on? I ask because if you want to add a battery backup to that internal RTC you have to run the whole PIC off the battery, sleep it, and divide that power from everything else (like the LCD). That's such a pain to do I just add an external RTC chip that runs off it's own battery. When my PIC starts up it loads it's RTC from the external one.

Joined Feb 9, 2012
122
Thanks for the reply.. It will be battery powered (probably from a 9V battery) but with the option to be powered by 12V from a small car battery which is already powering the 12V motor and being charged by a small solar panel -- I'll need to figure out a way to ensure someone doesn't try to use both 9V & 12V at the same time.

Our current timer we're using drives an LCD display continuously (the display never shuts off) but I was thinking it could be designed to sleep the display after perhaps 1 minute or so of inactivity (no keypresses). The current timer is also very inaccurate so I'm looking for something that's a bit better.. One thing I was thinking I could do to make it a little more accurate (and which would allow me to use the same board for other projects around the house) would be to integrate a WIFI module such as the Roving Networks RN171 or the Microchip MRF24Wb0MA both which could be used to do external time sync's once a week to a local WIFI network.

For the display, I was thinking about something like one of these seen here -- they're nice-n-cheap, have high contrast for outdoor reading but also offer backlighting in the off-chance you need to read it at night -- but I'm open to other suggestions. In terms of what will be displayed, it's your run of the mill set the current day of week/time (like those little mon/tues/wed indicators at the top like on a sprinkler controller), and setting trigger times for at least twice a day and perhaps more.

I didn't think about the timer issue you mention -- since it's going to primary run off some sort of battery, I'd love to keep it super-low power (perhaps making it able to run off a single 9V battery for a year??) and sleep 99.9% of the time drawing mere μA's (or below if I can get it into deep sleep mode most of the time but still wake up when a button is pressed -- something I'd hope would generate some sort of interrupt) from the battery.. Ultimately if I want to keep the price low, I'd find a way to work with the RTCC that's built-in since the DS1337 (or similar) aren't cheap unless you buy in large quantities. Unfortunately I wasn't able to find any information on the display that says it draws X power when the display is shut-off via a command.. I suppose I could control the power to the module via the microcontroller..??

P.S. I forgot to mention that the controller will need to power a relay of some sort to power the motor when the desired time arrives -- I was looking into different relays in a separate thread -- perhaps that relay will be my power-sucker in the whole equation..

Last edited:

#### ErnieM

Joined Apr 24, 2011
8,183
All I see on your display link is a LED backlight. Microchip has plenty of free tools and library code for certain devices. If one of these is listed or compatable you can do some cool stuff, especially if you can get a touch screen overlay (hint hint hint).

The PIC *can* run most of the time in sleep mode while keeping the RTC going, the alarm on the RTC can even wake it back up. The problem is the other parts in the system, making them also "sleep" (use little to no power). Here some MOSFET switches may help.

And as I am sure they are telling you on the other thread, relays will drain a battery really fast. A MOSFET switch would be way better.

9V and 12V? I would give thought on just using the 12V for both motor & micro.

Joined Feb 9, 2012
122

As for the display, I checked on the Microchip site but didn't see anything in particular suggesting particular LCD hardware -- I did read thru some of their library packages.. I'm assuming if I go that route, I'd need to use one of their LCD PIC24F's that would take care of some of the interfacing crud?

I'm thinking that since I'll be displaying small stuff I'd probably go with a display that can handle graphic stuff and/or fonts that I can fool with to reduce or increase fonts for some parts of the display instead of using a mere character display which I think will be limiting in this case.

As for alternates that have their own built-in controllers, how about this one which has a touchscreen option for about $12 with the main display costing perhaps$10 in onsies :
http://www.lcd-module.de/eng/pdf/grafik/dogs102-6e.pdf

Here's another non-touchscreen model that looks like it would be easily readible in sunlight or has a backlight for night reads if needed.. although it requires a parallel interface to drive it -- any suggestions on whether parallel is better in this case for LCD displays or SPI? I gather there are speed issues with using SPI for displays like these that need lots of data sent to them -- perhaps a parallel display is better? If that's the case, should I switch to one of the PICs that has the LCD features or ??

I like the idea of the MOSFETs as a relay alternative and will put them on my research list. I'll also consider the single voltage suggestion as well since it would reduce complexity which is always good..

#### ErnieM

Joined Apr 24, 2011
8,183
I'm not familiar with "LCD PIC24's" so I am assuming they have some special hardware for running segment displays like you see on a voltmeter. Since I don't do that I've never looked for or at them. Those wonderful color touchscreen displays don't require a custom micro to run. They either work off an 8 or 16 bit data word or something like the SPI bus (4 wires). Parallel is faster, SPI more compact. Either should work fine for you.

I have one here running on a PIC32 using the PMP interface, I believe some PIC24's have that too, but it is not essential.

If you limit yourself to the devices they support the Microchip Graphics Library has everything you want: any sort of image or font along with many of the controls you are using right now under Windows. When creating programs you can use bitmaps right from your PC, and even convert the PC fonts to things that work on your display. There is a large learning curve to getting this working, so you may be better to start with...

The display from Mouser is quite common and exceptionally simple to run. It has several modes so it can run off either 7 or 11 lines, there is sample code everywhere to run them. Microchip has examples in several places. You would never notice the time to update those displays, they will just change to the new data. For less power you would want a way to turn it on and off.

Joined Feb 9, 2012
122
Thanks Ernie! In talking with my wife this evening about our current timer and it's issues, it dawned on me that we could greatly simplify this project by ditching the display and need for the RTCC among others and use a remote light sensor instead that will trigger the relay at a certain level of light (day break) and trigger it again at dusk using some sort of light sensor similar to one of these.. The downside is how to mount one of these parts -- I'm a bit clueless on that angle..

Does this seem do-able? Perhaps I've over simplifying...

#### ErnieM

Joined Apr 24, 2011
8,183
Does this seem do-able? Perhaps I've over simplifying...
Heck no, I'm all for simple! Besides, a clock to turn on when the sun comes up needs regular adjustment as the sun is always coming and going at different times every day.

Elsewhere you stated:
...It will be battery powered (probably from a 9V battery) but with the option to be powered by 12V from a small car battery which is already powering the 12V motor and being charged by a small solar panel...
I'm backing up trying to understand what your needs are. Step one is to understand what needs to be done, then to work out a system to do that.

What I'm gathering is you have a solar powered system with a battery to store power, on a system that is only on when the sun is up. So is the motor running all day long? What is the battery for, saving power for (literally) rainy days?

If so, you already have a very nice sun sensor: the solar cell itself! The very latest garden lights do just that: originally they had a solar cell and a separate light sensor but the newest generation (last season) someone developed a custom IC that re-used the solar cell as the light sensor.

So your total system could be as simple as a solar cell charging the battery, and a voltage sensor that sees the sun (by monitoring the solar cell) to kick on the motor only when the sun is up.

Holler back with your overall needs. Perhaps this isn't a micro controller project at all, though perhaps a micro could spend most of it's time in sleep mode to help reduce overall power.

Joined Feb 9, 2012
122
This is a timer primarily that runs twice daily -- once at sun-up and once at sun-down. Each time it runs it must turn on a switch (relay,MOSFET,etc) for 30 seconds and then it's sleep time for another ~12 hours to do it all again.. That's about it. The power to be switched is 12VDC at 1A to run a motor. The power for that comes from a small 12V car battery that is topped off daily by a small solar panel. I think that's about it.

I'll have to admit that I'm more comfortable with microcontrollers and whatnot but if there's an easier solution, I'm fine with that as long as I can wrap my brain around it.

#### nickelflipper

Joined Jun 2, 2010
280
All that is needed is a simple solar tracker circuit with the addition of a 555 to give you the 30 second timer. Here is a circuit that I used for my tracker. A light detector circuit would need to be added. Then the comparator outputs could then be diode OR'd to trigger the 555 circuit to turn on the relay coil. Q3 and Q4 could be ignored, or used instead of the diodes to trigger the 555 one shot circuit.

#### Attachments

• 133.7 KB Views: 51

Joined Feb 9, 2012
122
My only reservation with a system like this (no MCU -- driven entirely by light levels) is what happens in poor weather such as overcast skies and whatnot.. In these situations the circuit should continue to behave as if it was a sunny day.. Is this something that might work or would it just be a matter of tuning the logic via a potentiometer or similar to basically operate anytime it starts seeing any sort of lighting? Seems like this may not be a good way to go as there are some situations where it may misbehave -- what might happen if the neighbors turn on a flashlight or spot light and accidentally hit the sensor -- I'm assuming it would probably trigger the logic to open the door when it shouldn't.. Hmm.. I might have to ponder this further..

#### nickelflipper

Joined Jun 2, 2010
280
My only reservation with a system like this (no MCU -- driven entirely by light levels) is what happens in poor weather such as overcast skies and whatnot.. In these situations the circuit should continue to behave as if it was a sunny day.. Is this something that might work or would it just be a matter of tuning the logic via a potentiometer or similar to basically operate anytime it starts seeing any sort of lighting? Seems like this may not be a good way to go as there are some situations where it may misbehave -- what might happen if the neighbors turn on a flashlight or spot light and accidentally hit the sensor -- I'm assuming it would probably trigger the logic to open the door when it shouldn't.. Hmm.. I might have to ponder this further..
The dark sensor circuit didn't seem to be a problem. The pot adjust could really dial in a very dark level to trigger. The light sensor circuit would be another matter, if car lights, flashlights, porch lights, or bright nearby street lights were involved. I had a streetlight on the neighbors property, but was able to tune it out with the pot. The cost of the light sensor, and any additional logic, would cancel out savings from the external rtc. Forgot you were trying to run an lcd, or glcd, with this too,...back to the micro .

I believe Microchip makes an 8 bit part with a real time clock calender, if that matters or not.

#### ErnieM

Joined Apr 24, 2011
8,183
This is a timer primarily that runs twice daily -- once at sun-up and once at sun-down. Each time it runs it must turn on a switch (relay,MOSFET,etc) for 30 seconds and then it's sleep time for another ~12 hours to do it all again.. That's about it. The power to be switched is 12VDC at 1A to run a motor. The power for that comes from a small 12V car battery that is topped off daily by a small solar panel. I think that's about it.

I'll have to admit that I'm more comfortable with microcontrollers and whatnot but if there's an easier solution, I'm fine with that as long as I can wrap my brain around it.
Excellent, that seems to completely define the system requirements. It sounds like you're going to make something like an automatic chicken coop door

I'm also comfortable with microcontrollers. To enable the motor at sun up and sun down will take a clock and a calendar that has a look-up table for sun rise and sun set. You can make that table from many online sights ((one example) so you can keep your "sun lock" on cloudy days.

The "hard part" of making the clock is making a display and some buttons to set it. A 2 line 8 character display should be ample for this. To save power this should be switched off most of the time.

Batteries are rated in Amp-Hours, how many amps can be drawn for how many hours. The load power is only 1A for a minute each day or 1A * 1/60 Hours = 0.017 Amp-Hours so this is a small load easily backed up by a small solar cell. The larger load may well be the micro: 0.017 AH is only 0.7mA when running 24 hours. Since the motor is only on for a brief period using a relay is OK as it just isn't on long enough to be an energy drain. However, the actually selection of a battery isn't my thing, I have no experience with batteries or solar cells, so you or others should make that pick.

#### CraigHB

Joined Aug 12, 2011
127
I like the 16 bit PICs a lot. I'm using a 24F for the project I'm currently working on.

The instruction words are 24 bits wide so a part with 16k of program memory can hold somewhat over 5k instructions. That's usually plenty to drive a 16x2 text mode display, handle some button pushes, and hold a couple tables. My project does just that and I'm only using half of the available 16k of program memory.

The newer 8 bit PICs have many of the features that the 16 bit ones have, but the 16 bit parts have table read instructions that make accessing lookup tables in program memory much easier to implement. Typically, a data point requires an instruction, so for example, a 10 bit ADC table would require 1024 instructions. You can store 16 bits of data in each instruction so you can cut that in half if you only need to store 8 bit data points.

Handling sleep and wake modes is pretty easy to do with the PICs. It should be no problem to put the device to sleep and into an ultra-low current mode when it's not active. There's plenty of options for which modules you leave running and which you shut down.

The 16 bit parts are designed to sleep with the RTCC and timers running. Any interrupt including input change notification can be used to wake them. My current project only draws 3uA in sleep mode. It wakes only in response to button pushes, but you can also run a timer in utra-low power mode for a periodic state check. There's also the newer 16 bit parts that have ultra-low draw that do even much better than that.

Joined Feb 9, 2012
122
Thanks for the comments guys! Ernie, you're spot on for the end-result! I guess I'm still interested in the micro side since I can use a single board for multiple projects. I'll probably do as you suggest and stick with the super-cheap 2x8 line display and a few buttons. Now off to play around with some schematic entry in DipTrace.. Once I get to a certain point, I'll probably run the schematic by you all if you don't mind so ensure I'm on the right path.. Hope that's OK.

Joined Feb 9, 2012
122
Hi all again -- just checking in -- haven't done a bit on this since our last discussion months ago! .. However, I did install Eagle PCB and ran through the tutorial the other night.. I ditched DipTrace since it had some UI issues running on the Mac that bothered me, but I did like it better than Eagle -- if I were using a PC I'd probably stick with diptrace. Anyway, I digress..

So, I've picked my PIC of choice - the PIC24HJ128GP202.. I'd ultimately like to run it for about a year from something like a 9V or series of AA batteries with it sleeping about 99% of the day as I mentioned above. The PIC will be running an LCD display which will be shutoff when sleeping..

In doing some homework, I gather a typical 9V battery has about 0.55Ah of charge capacity in it (perhaps this is out then!), and a single AA battery has about 2.4Ah so perhaps its better suited to this task.. I also checked the spec sheet for the pic processor and it appears that it's consumption in power-down mode (?) is somewhere between 24μA and 350μA (not sure how to read the chart on page 288 ) and will need to use the RTC obviously which I believe consumes additional current.

Anyway, does this sound do-able -- running this PIC24 that will literally be sleeping 99.9% (it'll only be awake for programming via buttons/lcd or when it's time to do work -- for ~1 minute out of the day)?

If it can't be run for a year from a set of batteries, I could make it require a 12VDC external source since it'll be wired up to other devices anyway..

Am I off-base with my thinking? Anyone used a PIC24 on batteries and if so, how long does it run before they ran dry (and under what circumstances)?

#### ErnieM

Joined Apr 24, 2011
8,183
I did a "for fun" app using a PIC10-something off a coin cell to sleep for long life. It never seemed to run for as long as I thought it would, but I might have got light one it. (It was a fridge door-open detector, came alive with light, beeped a few seconds later. In the dark it slept.)

I would not go doubling up on batteries. If you already have a big 12V battery getting recharged via solar each day then I would run the micro off that too. It does complicate things as the regulator for going from 12 to 5V will probably take more power then a sleeping PIC.