Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. nRF5 action!

nRF5 action!

Scheduled Pinned Locked Moved My Project
1.9k Posts 49 Posters 637.3k Views 44 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NeverDieN NeverDie

    @Mike_Lemo said in nRF5 Bluetooth action!:

    Check on adafruits feather reference they use two resistors as a voltage divider for that simple and effective.

    I'm not finding it. Do you have a link?

    M Offline
    M Offline
    Mike_Lemo
    wrote on last edited by
    #426

    @NeverDie https://learn.adafruit.com/assets/39913

    Top right corner of the schematic.

    1 Reply Last reply
    0
    • NeverDieN NeverDie

      @Mike_Lemo said in nRF5 Bluetooth action!:

      Check on adafruits feather reference they use two resistors as a voltage divider for that simple and effective.

      I'm not finding it. Do you have a link?

      M Offline
      M Offline
      Mike_Lemo
      wrote on last edited by
      #427

      @NeverDie but a much better but more expensive solution would be a lipo fuel guage from spark fun they use a Max chip for that.

      1 Reply Last reply
      0
      • Nca78N Offline
        Nca78N Offline
        Nca78
        Hardware Contributor
        wrote on last edited by
        #428

        If supplied with less than 3.6V you can do it with ADC, 1.2V voltage reference and 1/3 prescaling.
        But I've only looked at the theory yet.

        Rules are Vdd+0.3V max at the pin, and max 2.4V as input of ADC (after prescaling)

        d00616D 1 Reply Last reply
        2
        • NeverDieN Offline
          NeverDieN Offline
          NeverDie
          Hero Member
          wrote on last edited by NeverDie
          #429

          Well, as you all know, on the atmega328p you can read the 1.1v gap voltage using the battery voltage as the reference voltage, by doing analogRead(A0), and from just that one measurement then calculate the battery voltage by doing a little bit of math. So, I'm just wondering what the code is to do the equivalent of that (using 1.2v instead of 1.1v) on the nRF52832.

          1 Reply Last reply
          0
          • Nca78N Nca78

            If supplied with less than 3.6V you can do it with ADC, 1.2V voltage reference and 1/3 prescaling.
            But I've only looked at the theory yet.

            Rules are Vdd+0.3V max at the pin, and max 2.4V as input of ADC (after prescaling)

            d00616D Offline
            d00616D Offline
            d00616
            Contest Winner
            wrote on last edited by
            #430

            @Nca78 said in nRF5 Bluetooth action!:

            If supplied with less than 3.6V you can do it with ADC, 1.2V voltage reference and 1/3 prescaling.
            But I've only looked at the theory yet.

            For this, you can use the implemented hwCPUVoltage() function. Reading the voltage costs nRF51: 260µA/20µs | nRF52: 700µA/3µs

            NeverDieN 1 Reply Last reply
            2
            • NeverDieN Offline
              NeverDieN Offline
              NeverDie
              Hero Member
              wrote on last edited by NeverDie
              #431

              @d00616 said in nRF5 Bluetooth action!:

              hwCPUVoltage()

              I'm finally installing Visual Micro, because I hope it will help me quickly find where all these functions are defined. With all these new layers, the Arduino IDE is just no longer cutting it.

              NeverDieN scalzS TerrenceT 3 Replies Last reply
              1
              • NeverDieN NeverDie

                @d00616 said in nRF5 Bluetooth action!:

                hwCPUVoltage()

                I'm finally installing Visual Micro, because I hope it will help me quickly find where all these functions are defined. With all these new layers, the Arduino IDE is just no longer cutting it.

                NeverDieN Offline
                NeverDieN Offline
                NeverDie
                Hero Member
                wrote on last edited by NeverDie
                #432

                LOL. Well, Visual Micro found it alright, but just in the wrong place. It found it in MyHwAVR.cpp

                1 Reply Last reply
                0
                • NeverDieN NeverDie

                  @d00616 said in nRF5 Bluetooth action!:

                  hwCPUVoltage()

                  I'm finally installing Visual Micro, because I hope it will help me quickly find where all these functions are defined. With all these new layers, the Arduino IDE is just no longer cutting it.

                  scalzS Offline
                  scalzS Offline
                  scalz
                  Hardware Contributor
                  wrote on last edited by
                  #433

                  @NeverDie said in nRF5 Bluetooth action!:

                  @d00616 said in nRF5 Bluetooth action!:

                  hwCPUVoltage()

                  I'm finally installing Visual Micro, because I hope it will help me quickly find where all these functions are defined. With all these new layers, the Arduino IDE is just no longer cutting it.

                  good choice :+1:
                  for nrf5, the function is located in MyHwNRF5.cpp

                  1 Reply Last reply
                  1
                  • d00616D d00616

                    @Nca78 said in nRF5 Bluetooth action!:

                    If supplied with less than 3.6V you can do it with ADC, 1.2V voltage reference and 1/3 prescaling.
                    But I've only looked at the theory yet.

                    For this, you can use the implemented hwCPUVoltage() function. Reading the voltage costs nRF51: 260µA/20µs | nRF52: 700µA/3µs

                    NeverDieN Offline
                    NeverDieN Offline
                    NeverDie
                    Hero Member
                    wrote on last edited by NeverDie
                    #434

                    @d00616 said in nRF5 Bluetooth action!:

                    For this, you can use the implemented hwCPUVoltage() function.

                    I tried this function call on an nRF52 DK, and it seems to work. I then tried it on an Ebyte module, treated as an nRF52 DK "board", and it reported zero voltage. So, probably I just need to do a pin mapping so that it reads the voltage on the proper pin. But which pin/mapping would it be? I thought that Vcc wouldn't really be mappable to anything but Vcc. I guess whichever analog pin (if that's what it is?) is connected to Vcc on the nRF52 DK is the pin I need to find and re-map to its equivalent pin on the Ebyte module. Hmmm.... I'll have to look into which one that would be.

                    d00616D 1 Reply Last reply
                    0
                    • NeverDieN Offline
                      NeverDieN Offline
                      NeverDie
                      Hero Member
                      wrote on last edited by NeverDie
                      #435

                      Actually, something different may be going on. Here's the function definition:

                      uint16_t hwCPUVoltage()
                      {
                      	// VDD is prescaled 1/3 and compared with the internal 1.2V reference
                      	Serial.println("Inside hwCPUVoltage function.");
                      #if defined(NRF_ADC)
                          Serial.println("This is an NRF_ADC.");
                      	// NRF51:
                      	// Sampling is done with lowest resolution to minimize the time
                      	// 20uS@260uA
                      
                      	// Concurrent ressource: disable
                      	uint32_t lpcomp_enabled = NRF_LPCOMP->ENABLE;
                      	NRF_LPCOMP->ENABLE = 0;
                      
                      	// Enable and configure ADC
                      	NRF_ADC->ENABLE = 1;
                      	NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) |
                      	                  (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) |
                      	                  (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
                      	                  (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
                      	                  (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos);
                      	NRF_ADC->EVENTS_END = 0;
                      	NRF_ADC->TASKS_START = 1;
                      	while(!NRF_ADC->EVENTS_END);
                      	NRF_ADC->EVENTS_END = 0;
                      	int32_t sample = (int32_t)NRF_ADC->RESULT;
                      	NRF_ADC->TASKS_STOP = 1;
                      	NRF_ADC->ENABLE = 0;
                      
                      	// Restore LPCOMP state
                      	NRF_LPCOMP->ENABLE = lpcomp_enabled;
                      
                      	return (sample*3600)/255;
                      
                      #elif defined(NRF_SAADC)
                      	// NRF52:
                      	// Sampling time 3uS@700uA
                      	Serial.println("This is an NRF_SAADC.");
                      	int32_t sample;
                      	NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;
                      	NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_8bit << SAADC_RESOLUTION_VAL_Pos;
                      	NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_VDD << SAADC_CH_PSELP_PSELP_Pos;
                      	NRF_SAADC->CH[0].CONFIG = (SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos) |
                      	                          (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) |
                      	                          (SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) |
                      	                          (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) |
                      	                          (SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) |
                      	                          (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos) |
                      	                          (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos);
                      	NRF_SAADC->OVERSAMPLE = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass << SAADC_OVERSAMPLE_OVERSAMPLE_Pos;
                      	NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
                      	NRF_SAADC->RESULT.MAXCNT = 1;
                      	NRF_SAADC->RESULT.PTR = (uint32_t)&sample;
                      
                      	NRF_SAADC->EVENTS_STARTED = 0;
                      	NRF_SAADC->TASKS_START = 1;
                      	while (!NRF_SAADC->EVENTS_STARTED);
                      	NRF_SAADC->EVENTS_STARTED = 0;
                      
                      	NRF_SAADC->EVENTS_END = 0;
                      	NRF_SAADC->TASKS_SAMPLE = 1;
                      	while (!NRF_SAADC->EVENTS_END);
                      	NRF_SAADC->EVENTS_END = 0;
                      
                      	NRF_SAADC->EVENTS_STOPPED = 0;
                      	NRF_SAADC->TASKS_STOP = 1;
                      	while (!NRF_SAADC->EVENTS_STOPPED);
                      	NRF_SAADC->EVENTS_STOPPED = 1;
                      
                      	NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);
                      
                      	return (sample*3600)/255;
                      #else
                      	Serial.println("Unknown MCU!!");
                      	// unknown MCU
                      	return 0;
                      #endif
                      }
                      

                      One, perhaps likely, theory would be that it doesn't recognize the MCU, which would explain why it returns the value of zero. Well, to debug that, I added the Serial.println(...) statements into the library code (see above) in an attempt to see which of the if-else branches is being taken, but none of the Serial.println(...)'s were printed! Here's a sample of the output from the Ebyte Module:

                      332641 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                      332968 TSF:MSG:READ,0-0-255,s=255,c=3,t=8,pt=1,l=1,sg=0:0
                      332973 TSF:MSG:FPAR OK,ID=0,D=1
                      334649 TSM:FPAR:OK
                      334650 TSM:ID
                      334651 TSM:ID:REQ
                      334654 TSF:MSG:SEND,255-255-0-0,s=59,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      336662 TSM:ID
                      336663 TSM:ID:REQ
                      336665 TSF:MSG:SEND,255-255-0-0,s=23,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      338673 TSM:ID
                      338674 TSM:ID:REQ
                      338676 TSF:MSG:SEND,255-255-0-0,s=242,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      340684 TSM:ID
                      340685 TSM:ID:REQ
                      340687 TSF:MSG:SEND,255-255-0-0,s=205,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      342695 !TSM:ID:FAIL
                      342696 TSM:FAIL:CNT=7
                      342698 TSM:FAIL:DIS
                      342700 TSF:TDI:TSL
                      402703 TSM:FAIL:RE-INIT
                      402705 TSM:INIT
                      402706 TSM:INIT:TSP OK
                      402708 TSM:FPAR
                      402711 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                      403472 TSF:MSG:READ,0-0-255,s=255,c=3,t=8,pt=1,l=1,sg=0:0
                      403478 TSF:MSG:FPAR OK,ID=0,D=1
                      404719 TSM:FPAR:OK
                      404720 TSM:ID
                      404721 TSM:ID:REQ
                      404724 TSF:MSG:SEND,255-255-0-0,s=241,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      406732 TSM:ID
                      406733 TSM:ID:REQ
                      406735 TSF:MSG:SEND,255-255-0-0,s=205,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      408743 TSM:ID
                      408744 TSM:ID:REQ
                      408747 TSF:MSG:SEND,255-255-0-0,s=168,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      410754 TSM:ID
                      410755 TSM:ID:REQ
                      410757 TSF:MSG:SEND,255-255-0-0,s=131,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      412765 !TSM:ID:FAIL
                      412766 TSM:FAIL:CNT=7
                      412768 TSM:FAIL:DIS
                      412770 TSF:TDI:TSL
                      472773 TSM:FAIL:RE-INIT
                      472775 TSM:INIT
                      472776 TSM:INIT:TSP OK
                      472778 TSM:FPAR
                      472781 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                      472955 TSF:MSG:READ,0-0-255,s=255,c=3,t=8,pt=1,l=1,sg=0:0
                      472960 TSF:MSG:FPAR OK,ID=0,D=1
                      474789 TSM:FPAR:OK
                      474790 TSM:ID
                      474791 TSM:ID:REQ
                      474794 TSF:MSG:SEND,255-255-0-0,s=167,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      476802 TSM:ID
                      476803 TSM:ID:REQ
                      476805 TSF:MSG:SEND,255-255-0-0,s=131,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      478813 TSM:ID
                      478814 TSM:ID:REQ
                      478816 TSF:MSG:SEND,255-255-0-0,s=94,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      480824 TSM:ID
                      480825 TSM:ID:REQ
                      480827 TSF:MSG:SEND,255-255-0-0,s=57,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                      482835 !TSM:ID:FAIL
                      482836 TSM:FAIL:CNT=7
                      482838 TSM:FAIL:DIS
                      482840 TSF:TDI:TSL
                      

                      I'm not quite sure how to interpret that, but pretty clearly it doesn't contain the println's that I was expecting.

                      Any theories as to what's going on?

                      1 Reply Last reply
                      0
                      • NeverDieN Offline
                        NeverDieN Offline
                        NeverDie
                        Hero Member
                        wrote on last edited by
                        #436

                        Bracketing that and setting it aside, I do now notice this line in the BatteryPoweredSensor demo sketch:

                        int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                        

                        So, if I map that A0 in the sketch to the A0 of the Ebyte Module, then maybe (hopefully) the voltage measurement will work on the Ebyte Module. I'll give it a try.

                        1 Reply Last reply
                        0
                        • NeverDieN Offline
                          NeverDieN Offline
                          NeverDie
                          Hero Member
                          wrote on last edited by NeverDie
                          #437

                          Unfortunately, after I add:

                          #include <mySensors.h>
                          

                          the locus of control goes somewhere else (I guess the gateway or something?). Anyhow, it makes this very hard to debug.

                          For instance, the pin of interest is PIN_AIN0. I can't find where it's defined, and I can't print out its value either, because of this locus of control issue.

                          Anyhow, I think I may wait for others to get up and running with their modules, and start facing the same issues. Maybe then we can help each other figure this stuff out.

                          1 Reply Last reply
                          1
                          • d00616D Offline
                            d00616D Offline
                            d00616
                            Contest Winner
                            wrote on last edited by
                            #438

                            @NeverDie I don't know why the println() doesn't work. There is an "DEBUG_OUTPUT(x, ##VA_ARGS)" macro, you can use when debug is enabled.

                            NRF_ADC is the nRF51 ADC and NRF_SAADC is the ADC of the nRF52. They are defined when "nrf.h" is included.

                            What board type have use used for your tests?

                            NeverDieN 2 Replies Last reply
                            1
                            • d00616D d00616

                              @NeverDie I don't know why the println() doesn't work. There is an "DEBUG_OUTPUT(x, ##VA_ARGS)" macro, you can use when debug is enabled.

                              NRF_ADC is the nRF51 ADC and NRF_SAADC is the ADC of the nRF52. They are defined when "nrf.h" is included.

                              What board type have use used for your tests?

                              NeverDieN Offline
                              NeverDieN Offline
                              NeverDie
                              Hero Member
                              wrote on last edited by
                              #439

                              @d00616 said in nRF5 Bluetooth action!:

                              What board type have use used for your tests?

                              The nRF52832 Ebyte Module.

                              1 Reply Last reply
                              0
                              • NeverDieN NeverDie

                                @d00616 said in nRF5 Bluetooth action!:

                                hwCPUVoltage()

                                I'm finally installing Visual Micro, because I hope it will help me quickly find where all these functions are defined. With all these new layers, the Arduino IDE is just no longer cutting it.

                                TerrenceT Offline
                                TerrenceT Offline
                                Terrence
                                wrote on last edited by
                                #440

                                @NeverDie VS Code with the Arduino Extension is your friend.

                                NeverDieN 1 Reply Last reply
                                1
                                • d00616D d00616

                                  @NeverDie I don't know why the println() doesn't work. There is an "DEBUG_OUTPUT(x, ##VA_ARGS)" macro, you can use when debug is enabled.

                                  NRF_ADC is the nRF51 ADC and NRF_SAADC is the ADC of the nRF52. They are defined when "nrf.h" is included.

                                  What board type have use used for your tests?

                                  NeverDieN Offline
                                  NeverDieN Offline
                                  NeverDie
                                  Hero Member
                                  wrote on last edited by NeverDie
                                  #441

                                  @d00616 said in nRF5 Bluetooth action!:

                                  What board type have use used for your tests?

                                  I just re-read your question and realized you were asking something else than the question that I answered above.

                                  Answer: nRF52 DK is the board type, because I wired directly to P0.06 as its Tx pin on the Ebyte nRF52832 Module.

                                  1 Reply Last reply
                                  0
                                  • TerrenceT Terrence

                                    @NeverDie VS Code with the Arduino Extension is your friend.

                                    NeverDieN Offline
                                    NeverDieN Offline
                                    NeverDie
                                    Hero Member
                                    wrote on last edited by NeverDie
                                    #442

                                    @Terrence said in nRF5 Bluetooth action!:

                                    VS Code with the Arduino Extension is your friend.

                                    I'll try that. Maybe it will be a little less of a learning curve than Visual Micro yet still do what I need.

                                    1 Reply Last reply
                                    0
                                    • NeverDieN Offline
                                      NeverDieN Offline
                                      NeverDie
                                      Hero Member
                                      wrote on last edited by NeverDie
                                      #443

                                      I just now did a brute force hack of the MySensors BatteryPoweredSensor sketch, where I copied and renamed the hwCPUVoltage function from the library and then called it from within a tight loop so that I never lose the locus of control. It worked!

                                      /**
                                       * The MySensors Arduino library handles the wireless radio link and protocol
                                       * between your home built sensors/actuators and HA controller of choice.
                                       * The sensors forms a self healing radio network with optional repeaters. Each
                                       * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                                       * network topology allowing messages to be routed to nodes.
                                       *
                                       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                                       * Copyright (C) 2013-2015 Sensnology AB
                                       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                                       *
                                       * Documentation: http://www.mysensors.org
                                       * Support Forum: http://forum.mysensors.org
                                       *
                                       * This program is free software; you can redistribute it and/or
                                       * modify it under the terms of the GNU General Public License
                                       * version 2 as published by the Free Software Foundation.
                                       *
                                       *******************************
                                       *
                                       * DESCRIPTION
                                       *
                                       * This is an example that demonstrates how to report the battery level for a sensor
                                       * Instructions for measuring battery capacity on A0 are available here:
                                       * http://www.mysensors.org/build/battery
                                       *
                                       */
                                      
                                      
                                      
                                      // Enable debug prints to serial monitor
                                      //#define MY_DEBUG
                                      
                                      // Enable and select radio type attached
                                      //#define MY_RADIO_NRF24
                                      //#define MY_RADIO_RFM69
                                      #define MY_RADIO_NRF5_ESB
                                      
                                      //#include <MySensors.h>
                                      
                                      int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                      
                                      //unsigned long SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
                                      unsigned long SLEEP_TIME = 1000;  // sleep time between reads (seconds * 1000 milliseconds)
                                      int oldBatteryPcnt = 0;
                                      
                                      uint16_t counter=0;
                                      uint16_t volts=0;
                                      
                                      uint16_t theHwCPUVoltage()
                                      {
                                        // VDD is prescaled 1/3 and compared with the internal 1.2V reference
                                        Serial.println("Inside hwCPUVoltage function.");
                                      #if defined(NRF_ADC)
                                          Serial.println("This is an NRF_ADC.");
                                        // NRF51:
                                        // Sampling is done with lowest resolution to minimize the time
                                        // 20uS@260uA
                                      
                                        // Concurrent ressource: disable
                                        uint32_t lpcomp_enabled = NRF_LPCOMP->ENABLE;
                                        NRF_LPCOMP->ENABLE = 0;
                                      
                                        // Enable and configure ADC
                                        NRF_ADC->ENABLE = 1;
                                        NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) |
                                                          (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) |
                                                          (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
                                                          (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
                                                          (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos);
                                        NRF_ADC->EVENTS_END = 0;
                                        NRF_ADC->TASKS_START = 1;
                                        while(!NRF_ADC->EVENTS_END);
                                        NRF_ADC->EVENTS_END = 0;
                                        int32_t sample = (int32_t)NRF_ADC->RESULT;
                                        NRF_ADC->TASKS_STOP = 1;
                                        NRF_ADC->ENABLE = 0;
                                      
                                        // Restore LPCOMP state
                                        NRF_LPCOMP->ENABLE = lpcomp_enabled;
                                      
                                        return (sample*3600)/255;
                                      
                                      #elif defined(NRF_SAADC)
                                        // NRF52:
                                        // Sampling time 3uS@700uA
                                        Serial.println("This is an NRF_SAADC.");
                                        int32_t sample;
                                        NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;
                                        NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_8bit << SAADC_RESOLUTION_VAL_Pos;
                                        NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_VDD << SAADC_CH_PSELP_PSELP_Pos;
                                        NRF_SAADC->CH[0].CONFIG = (SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos) |
                                                                  (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) |
                                                                  (SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) |
                                                                  (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) |
                                                                  (SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) |
                                                                  (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos) |
                                                                  (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos);
                                        NRF_SAADC->OVERSAMPLE = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass << SAADC_OVERSAMPLE_OVERSAMPLE_Pos;
                                        NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
                                        NRF_SAADC->RESULT.MAXCNT = 1;
                                        NRF_SAADC->RESULT.PTR = (uint32_t)&sample;
                                      
                                        NRF_SAADC->EVENTS_STARTED = 0;
                                        NRF_SAADC->TASKS_START = 1;
                                        while (!NRF_SAADC->EVENTS_STARTED);
                                        NRF_SAADC->EVENTS_STARTED = 0;
                                      
                                        NRF_SAADC->EVENTS_END = 0;
                                        NRF_SAADC->TASKS_SAMPLE = 1;
                                        while (!NRF_SAADC->EVENTS_END);
                                        NRF_SAADC->EVENTS_END = 0;
                                      
                                        NRF_SAADC->EVENTS_STOPPED = 0;
                                        NRF_SAADC->TASKS_STOP = 1;
                                        while (!NRF_SAADC->EVENTS_STOPPED);
                                        NRF_SAADC->EVENTS_STOPPED = 1;
                                      
                                        NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);
                                      
                                        return (sample*3600)/255;
                                      #else
                                        Serial.println("Unknown MCU!!");
                                        // unknown MCU
                                        return 0;
                                      #endif
                                      }
                                      
                                      
                                      void setup()
                                      {
                                        Serial.begin(115200);
                                        Serial.println("Setup procedure beginning.");
                                        while (true) {
                                          volts=theHwCPUVoltage();
                                          Serial.print("counter=");
                                          Serial.print(counter++);
                                          Serial.print(", hwCPUVoltage=");
                                          Serial.println(volts);
                                        }
                                      	// use the 1.1 V internal reference
                                      //#if defined(__AVR_ATmega2560__)
                                      //	analogReference(INTERNAL1V1);
                                      //#else
                                      //	analogReference(INTERNAL);
                                      //#endif
                                      }
                                      

                                      Here is a sample of the output:

                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5013, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5014, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5015, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5016, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5017, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5018, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5019, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5020, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5021, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5022, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5023, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5024, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5025, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5026, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5027, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5028, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5029, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5030, hwCPUVoltage=3289
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5031, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5032, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5033, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5034, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5035, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5036, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5037, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5038, hwCPUVoltage=3289
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5039, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5040, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5041, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5042, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5043, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5044, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5045, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5046, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5047, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      
                                      counter=5048, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5049, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5050, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5051, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5052, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5053, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5054, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5055, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5056, hwCPUVoltage=3303
                                      Inside hwCPUVoltage function.
                                      This is an NRF_SAADC.
                                      counter=5057, hwCPUVoltage=3303
                                      
                                      
                                      1 Reply Last reply
                                      0
                                      • NeverDieN Offline
                                        NeverDieN Offline
                                        NeverDie
                                        Hero Member
                                        wrote on last edited by
                                        #444

                                        Besides that it can now do println()'s, the other good news is that it looks like I won't need to change the pin assignment.

                                        Too bad the demo sketch only transmits one byte of voltage information, rather than two bytes.

                                        1 Reply Last reply
                                        0
                                        • NeverDieN NeverDie

                                          @d00616 said in nRF5 Bluetooth action!:

                                          For this, you can use the implemented hwCPUVoltage() function.

                                          I tried this function call on an nRF52 DK, and it seems to work. I then tried it on an Ebyte module, treated as an nRF52 DK "board", and it reported zero voltage. So, probably I just need to do a pin mapping so that it reads the voltage on the proper pin. But which pin/mapping would it be? I thought that Vcc wouldn't really be mappable to anything but Vcc. I guess whichever analog pin (if that's what it is?) is connected to Vcc on the nRF52 DK is the pin I need to find and re-map to its equivalent pin on the Ebyte module. Hmmm.... I'll have to look into which one that would be.

                                          d00616D Offline
                                          d00616D Offline
                                          d00616
                                          Contest Winner
                                          wrote on last edited by
                                          #445

                                          @NeverDie said in nRF5 Bluetooth action!:

                                          I tried this function call on an nRF52 DK, and it seems to work. I then tried it on an Ebyte module, treated as an nRF52 DK "board", and it reported zero voltage.

                                          I have tried the hwCPUVoltage() function with an Ebyte and an RedBear module. Both modules are reporting the voltage.

                                          NeverDieN 1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          27

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular