Working (so far) NRF5 NRF52832 PIR/Motion Sketch



  • So, like i've mentioned before, my coding is ugly but generally works. So if anyone wants to pretty this up - have at it.

    Finally circling back to the nrf5 that I haven't touched in like a year I spent some time trying to get a working sketch. I played with neverdie's LPCOMP one and it kept locking up or something and I tried again with a GPIOTE one and it (besides being too complicated for me in general) was doing something similar. Using the latest alpha branch with the fixes for the nrf5 that were added about a year ago it finally works with just the plain jane interrupt sketch with a small addition to put i2c asleep. Found that tidbit on the nordic forums and sprinkled around elsewhere as well.
    Power usage is around 24 microamps. I use it with a CR2450 so I figure probably at least 6 months. I'm using a circular board similar to @NeverDie 's nrf51 board so its a very compact PIR.

    As a side note, I tried making the LED blink but it would always lock up after sending a 0 (no pir low). no idea why so I stopped messing with it for now just happy that it's working

    Hopefully this is of use to someone:

    /*
     * This example sketch shows how you can manage the nRF5 pin mapping as part of your code.
     * You can use the method for any nRF51822 or nRF52832 board or module.
     * 
     * Most components, like UART, SPI, Wire Bus, of the nRF5 series chips don't
     * have a fixed pin mapping. There are some pins with restrictions like analog
     * inputs, NFC or pins near the radio module. Please refer the latest
     * documentation about pin restrictions at http://infocenter.nordicsemi.com 
     * 
     * To use the custom pin mapping you have to do following steps:
     * 
     * 1. Install "arduino-nrf5" like described at
     *    https://github.com/sandeepmistry/arduino-nRF5/
     * 2. Install the "My Sensors nRF5 Boards" with the board manager like
     *    explained at https://github.com/mysensors/ArduinoBoards
     * 3. Copy the files "MyBoardNRF5.cpp" and "MyBoardNRF5.h" from
     *    "MyBoardNRF5" example into your sketch.
     * 4. Modify pin mappings in "MyBoardNRF5.cpp" and "MyBoardNRF5.h" to fit
     *    your requirements.
     * 5. Select "MyBoardNRF5 nrf52832" or "MyBoardNRF5 nrf52822" as your board.
     *    Choose the correct parameters and programmer in the Tools menu.
     */
    
    // MySensors /////////////////////
    #define MY_RADIO_NRF5_ESB
    #define MY_NODE_ID 121
    //#define MY_NRF5_ESB_MODE (NRF5_1MBPS)
    #define MY_NRF5_ESB_MODE (NRF5_250KBPS)
    #define MY_PASSIVE_NODE
    #include <MySensors.h>
    #define SKETCH_NAME "NRF52 Motion 2021"
    #define SKETCH_VERSION "v1"
    #define CHILD_ID 1
    #define CHILD_ID_VOLT 28
    
    
    float batteryVoltage=0;  
    uint32_t SLEEP_TIME = 840000; //14 minutes
    int batteryReport = 0;
    int percent = 0;
    MyMessage msg(CHILD_ID, V_TRIPPED); // Motion
    MyMessage msgVoltage(CHILD_ID_VOLT,V_VOLTAGE);
    #define DIGITAL_INPUT_SENSOR 2
    
    
    void setup() { 
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);
      pinMode(LED_BUILTIN, OUTPUT);
      i2c_off(); // Shut down the i2c
    }
    
    void presentation()
    {
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      present(CHILD_ID, S_MOTION);
      present(CHILD_ID_VOLT,S_MULTIMETER);
    }
    
    void loop() 
    {
      bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      send(msg.set(tripped?"1":"0"));  // Send motion tripped value to gw
      i2c_off();
      sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
      if ( batteryReport == 0 ) {
        batteryVoltage=((float)hwCPUVoltage())/1000.0;  //take voltage measurement after transmission to hopefully measure lowest voltage that occurs. 
        percent = map(batteryVoltage, 2.8, 3.25, 0, 100);
        //send(msgVoltage.set(batteryVoltage,3));  //send battery voltage with 3 decimal places
        send(msgVoltage.set(percent,2));
      }
      batteryReport++;
      if ( batteryReport > 20 ) {
        batteryReport=0;
      }
    
    }
    
    void i2c_off() // Shut i2c down, powerconsumption rises up to 450uA instead of 11 ua  - https://github.com/sandeepmistry/arduino-nRF5/issues/291#issuecomment-407492282
    {
      NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
    *(volatile uint32_t *)0x40004FFC = 0;
    *(volatile uint32_t *)0x40004FFC;
    *(volatile uint32_t *)0x40004FFC = 1;
    
    }
    

Log in to reply
 

Suggested Topics

9
Online

11.2k
Users

11.1k
Topics

112.5k
Posts