How to power PIC24 with battery; SD (data logging)

Discussion in 'Embedded Systems and Microcontrollers' started by BryAB, Apr 21, 2016.

  1. BryAB

    Thread Starter New Member

    Apr 21, 2016
    5
    0
    I have developed an application that runs on a Microchip PIC microcontroller (PIC24HJ64GP502). This is actually a PIC24 (https://www.sparkfun.com/products/retired/9148) that I originally bought from Spark Fun and that no longer is commercially available. It contains a bootloader (Bully Bootloader) that allowed me to watch the real time change of variables (on my PC screen, via the USB connection between my PC and my PIC24) that were calculated by my application's algorithm.

    Let me explain basically what my present PIC24 does: A 3.3 volt heart rate pulse detector (worn on my finger tip: http://pulsesensor.com/) feeds an analog signal to my PIC's ADC peripheral (AN0 pin). A 3.3 volt accelerometer (MMA8452Q: https://www.sparkfun.com/products/12756) connects to my PIC uC via the I2C peripheral (SDA and SCL pins). A 32 KHz crystal along with two 22 pF capacitors connects to the SOSCI and SOSCO pins of my PIC24 to provide a real time clock and calendar (needed for time and date stamping my logged data).

    From my pulse sensor and accelerometer signals, my PIC24 (worn on my wrist while sleeping) determines when I experience REM sleep and sends pertinent data (heart rate and body motion events along with time and date) to my PC via the USB connection. This data (up to 4500 KB per night) is logged on my PC so that I can later evaluate it and attempt to improve my algorithm's ability to better detect REM episodes.

    I would like to power a similar PIC24 with a battery (instead of through USB as I do presently so as to be able to sleep without USB connection to my PC). I am not sure how to do this; in other words, which pins (e.g., Vdd, AVdd) would need to be connected to positive side of battery voltage (through a voltage regulator, I suspect) and which pins (e.g., Vss, AVss) would need to be connected to negative side of battery voltage? Where might I find pertinent information as to how to do set up a PIC24 for battery power? I have only been working with microcontroller boards that are powered through their USB connection to my PC, and so I am very unsure as to how to do this.

    Also, I would like to write my logged data to a micro SD card (instead of to my PC so as, again, to sleep without being connected to my PC). I have never written an application that uses an EEPROM or SD. Can anyone advise me as to which peripheral (e.g., SPI) to use and where I might find pertinent information on how to do this?

    I would be interested in any books or technical literature that anyone can recommend that might go into detail on how to do such things.
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,342
    1,024
    First off, you'll need to see what your current consumption is to determine what the battery capacity needs to be. If the battery is >3V3, you can consider a micropower regulator to maintain a stable 3V3 to the processor and a reference to the ADC.
    You will definitely need to hook up Vdd and AVdd to the battery powered 3V3 source and Vss, AVss to battery (-). You'll have to ensure that AVdd can't ever be > Vdd.

    If you are using a separate reference for the ADC, you might dispense with the 3V3 regulator since the processor will tolerate some variance of Vdd (not much though). You can dispense with both the reference and regulator if the battery voltage is 3.0-3.6V AND all analog signals are ratiometric i.e. the analog output is expressed as some fraction of Vdd rather than an absolute value.

    You can use an external EEPROM (SPI or I2C, I like the latter) for storage. Use a low power variant and take advantage of the write buffer on the EEPROM. Keep data internal on the PIC and the EE in low power mode until you have a buffer's worth of data then write that to the EEPROM in one operation. When the USB is reconnected, dump the EE. An SD card would work as well and has the advantage of portability but reading and writing is more involved. Since you have 8K of RAM on board, you might be able to just store locally and upchuck to USB when its connected without external storage. RAM is saved down to about 1.8Vdd. In that case, the brownout detector would be used to halt the PIC if the battery drooped below Vdd(min), maintaining the RAM as long as it didn't droop below 1.8V

    Power consumption will be an issue of course. Ch. 10 of the datasheet has the basics. See the tips n tricks attached for some additional ideas to curb power consumption.

    Good luck!
     
  3. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    Vdd goes to +, Vss goes to -. Your PIC needs 3.3V. If it's only for yourself, you probably can get away without a regulator if you use two 1.5V batteries, at least when they're new. Otherwise, you will need a regulator.

    EEPROM chip accessed by SPI is very easy to do. SD card is much harder.
     
  4. Picbuster

    Member

    Dec 2, 2013
    374
    50
    When using an external usb chip(eq FTDI) be aware that the input lines ( outputs from pic) have an internal pull up.
    Normally you will feed the usb chip direct from pc cable. however; when pic outputs are high current will flow.
    Solution; detect usb power on and put the associated pic outputs low wen usb is not needed.
     
  5. BryAB

    Thread Starter New Member

    Apr 21, 2016
    5
    0
    I hate to show my ignorance, but I will need to log about 4500 KB worth of data in one night. Are there EEPROMs available that will hold that much data?
     
  6. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
  7. BryAB

    Thread Starter New Member

    Apr 21, 2016
    5
    0
    I was just told by someone at Digi-Key that FLASH will interface with the PIC24's SPI peripheral as easily as EEPROM and that it has the kind of storage capacity and small physical size that I seek. In your experiences, is FLASH as easy to interface with the PIC24 as EEPROM? I know that FLASH has only about 100,000 read/write cycles, but that is sufficient for my purposes.
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,782
    360
    depending on your definition of "as easily" or "flash".

    More and more mcus now support self-programming and that allows flash to be used to store user data at run time. The issues here are 1) you have to page through flash; and 2) if you want to store 4500KB of data in flash, your choices are greatly limited.

    I think you will find that programming will be a challenge, from accessing data files and from low-power programming. I think it may be more doable if you have a data logger (portable or otherwise) triggered by a device of your design that detects whatever conditions you wish to design for.
     
  9. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    Flash and EEPROM is more or less the same thing.
     
  10. BryAB

    Thread Starter New Member

    Apr 21, 2016
    5
    0
    I see what you are saying. I have considered using the Adafruit Adalogger (an Arduino) instead of a PIC24 since the Adalogger has already resolved my data logging and battery-powered issues for me. However, I presently have an algorithm that works well on my PIC24 (which unfortunately must be connected to my PC for power and data logging), and the more I look into Arduino, the more I wonder if I will have to rewrite much of my C code in order to run it successfully on an Arduino (e.g., Arduino does not allow me to use serial communications reliably within my timer ISR as my PIC24 does via the printf() function). I am trying to determine which approach, PIC24 (with battery and data logging issues) or Adalogger (with rewriting my C code issues) will be less time consuming.
     
  11. RichardO

    Well-Known Member

    May 4, 2013
    1,230
    382
    4.5 MegaBytes sounds like an awful lot of data. Are you sure that is right?
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,138
    1,789
  13. dannyf

    Well-Known Member

    Sep 13, 2015
    1,782
    360
    If you want, you can certainly retain your pic24 and its algorithm as a trigger for a datalogger.

    If it is written in C, it is certainly quite easy to port.

    If you had used the printf() within the isr, it is in most cases (i'm trying to be conservative here) a very poor way of writing code.

    The biggest issue I have with arduino's uart module is that it is not interrupt driven. That would have made live much easier for others.
     
  14. Papabravo

    Expert

    Feb 24, 2006
    10,138
    1,789
    As an Engineering Manager I would fire ANY programmer on the spot for putting a printf() inside an ISR with the following epithet:

    "You block, you stone you worse than senseless thing -- YOU'RE FIRED!"

    The justification would be the fundamental misunderstanding of what interrupts are for. You just can't take a risk on people like that.
     
  15. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I only set flags in the interrupt handler return immediately. These are checked in the main loop.
     
  16. BryAB

    Thread Starter New Member

    Apr 21, 2016
    5
    0
    I am confident that it is 4500 KB because when I log it, that is what the logging program on my PC says. I am looking at heart rate, heart rate variance, body motion events, and several other variables (calculated within the ISR from heart rate data) that my algorithm calculates each minute so that I can detect REM episodes during sleep. I need to see and graph those values (printed each minute of a 6 to 8 hour period of sleep) to better detect REM sleep.

    I just began to learn how to program in C about 5 years ago as a part time hobby. I still have much to learn and would not dream of programming professionally. My skill level is nowhere near that, but I honestly could not see how to extract (and therefore view) my data outside of the ISR (when I learned that I would probably have to do that with the Arduino Adalogger). No doubt more experienced programmers would have little trouble doing that.


    Could you possibly explain a little more about what you mean here? Is there a "datalogger" device that will connect to my PIC24 and log its data?

    Thanks for all of the advice and interest, and please excuse my ignorance and inexperience.
     
Loading...