mBuino program that shows how to read VCC using the blink of a LED

Dependencies:   USBDevice mbed

mBuino PowerMeter

This program shows how to read the current voltage on the 3V3 (VCC) pin of mBuino using the blink of a LED.

Since mBuino uses the VCC voltage as reference for analog reads, all readings are relative. To get an absolute reading an external reference voltage is needed. An easy way to get an external reference is by using a zener-diode with proper characteristics.

Not having such a zener around made me look for alternatives. I found some on this page: http://www.talkingelectronics.com/projects/200TrCcts/200TrCcts.html#52

/media/uploads/maxint/reference_voltage_using_diode.jpg

Use a LED!

The most interesting method I found was to use the LEDs on mBuino! All needed to use this is an extra connection. I used a 100R resistor as connection between pin 14 and LED6. Pin 14 is the small dot right above the one of the version number. Alternatively you can also use an external LED or (zener)-diode. The picture below shows both the added resistor as well as an external LED/resistor circuit. /media/uploads/maxint/mbuino_powermeter.jpg

Serial USB

The picture above shows a USB cable connected to mBuino. When mBuino is powered via USB and nothing else is connected to VCC, power comes from the 3v3 regulator. The VCC reading will then be around its maximum. USB is used to display the readings on the USB serial console. Note that for this the USB serial driver and a terminal application such as Putty is required. When using the console the spacebar can be hit to switch between pin14+LED6 and pin15 with an external LED/resistor via pin4.

See the code comments for more information.

Files at this revision

API Documentation at this revision

Comitter:
maxint
Date:
Fri Aug 07 08:56:06 2015 +0000
Parent:
3:9e93f14db34d
Commit message:
added some comments

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Aug 02 18:22:37 2015 +0000
+++ b/main.cpp	Fri Aug 07 08:56:06 2015 +0000
@@ -3,7 +3,7 @@
 ** 
 ** This program shows how to read the current voltage on the 3V3 (VCC) pin of mBuino using the blink of a LED.
 **
-** Since mBuino uses the VCC voltage as reference for analag reads, all readings are relative.
+** Since mBuino uses the VCC voltage as reference for analog reads, all readings are relative.
 ** To get an absolute reading an external reference voltage is needed.
 ** An easy way to get an external reference is by using a zener-diode with proper characteristics.
 ** Not having such a zener around made me look for alternatives. I found some on this page:
@@ -35,7 +35,7 @@
     
     // Please note that the voltage on the 3v3 pin is always lower than 3.3V as diodes D10 and D11
     // are between the actual VCC of the 3.3V regulator and that of the CR2032 battery.
-    // Measured voltages are 2.85V when powerd by USB or lower when powered by battery.
+    // Measured voltages are 2.85V when powered by USB or lower when powered by battery.
     // When mBuino has its LEDs on, or when an external device is using VCC, the voltage will be lower too.
 
     // The current VCC voltage can be determined by using analogIn to read a known reference voltage.
@@ -120,26 +120,28 @@
     SweepAllLeds(7, false,0.02);
     SweepAllLeds();
 
+    // flash a led while waiting for console response as we 
     t.start();
-    while(!serialUSB.readable() && (t.read_ms() <= 3000)) {
+    while(!serialUSB.readable() && (t.read_ms() <= 3000))
+    {
         serialUSB.printf("."); 
         ledFlash(0);
         wait(0.1);
     }
 
-
-    ledFlash(0);
-    wait(0.5);
-
+    // show status on console and give instructions
     serialUSB.printf("\r\n=== mBuino PowerMeter ====  \r\n");
     serialUSB.printf("Measuring LED pin %d [%1.2fV] using pin %d\r\n", pinLed, ftVled, pinAnalog);
     serialUSB.printf("Hit spacebar to change to LED pin 4 [1.85V] using pin 15.\r\n");
+
+    // perform voltage readings, indicate voltage level and process serial console input
     while(true)
     {
         //ftVCC=readVCC();  // float readVCC(PinName nAnalogIn=P0_14, PinName nLed=LED6, int nBlinkDelayMsec=0, float ftVrefInit=1.91)
         ftVcc=readVCC(pinAnalog, pinLed, 0, ftVled);
         serialUSB.printf("VCC: %1.2fV\r\n", ftVcc);
 
+        // indicate voltage level using all 7 LEDs on mBuino
         if(ftVcc>ftVmin)
         {
             uint8_t uLed=0;
@@ -154,11 +156,12 @@
             wait(1);
         }
         else
-        {
+        {   // battery is very low: show rapid flashing
             ledFlash(0);
             wait(0.25);
         }
         
+        // process serial console input
         if(serialUSB.readable())
         {   // switch pins when is spacebar is hit
             if(serialUSB.getc()==' ')