Friday, November 28, 2014

Power monitoring with the Moteino -- Attempt #1

This week, in my regular time at the Makerspace, I experimented with the Moteino I recently purchased.  I want to use the Moteino as a temperature sensor in my Home Environmental Sensor Array (HESA).  One thing I would like the sensor to do is measure the voltage coming from the battery and report it back to the host.  This way, I will know when a battery on one of the sensors is about to die.

The Moteino is fully compatible with an Arduino UNO.  Vishal from the 'space told me that the Arduino has the ability to determine the incoming power.  There is an internal reference voltage that always measures 1.1v.  This reference voltage can be used to more accurately measure the input voltage to the Arduino.  Here is a web page that talks about this in more detail.  That web page also has code to measure the input voltage.  Below is the code that I used to measure the voltage on my Moteino.  The output consistently read 3369 millivolts.
long readVCC() {  // Read 1.1V reference against AVcc  // set the reference to Vcc and the measurement to the internal 1.1V reference  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)    ADMUX = _BV(MUX5) | _BV(MUX0);  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)    ADMUX = _BV(MUX3) | _BV(MUX2);  #else    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  #endif     delay(2); // Wait for Vref to settle  ADCSRA |= _BV(ADSC); // Start conversion  while (bit_is_set(ADCSRA,ADSC)); // measuring   uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH    uint8_t high = ADCH; // unlocks both   long result = (high<<8) | low;   result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000  return result; // Vcc in millivolts}
void setup() {  // initialize serial communication at 9600 bits per second:  Serial.begin(9600);}
void loop() {  float voltage = readVCC();  // print out the value you read:  Serial.println(voltage);  }
BTW, after uploading the program to the Arduino, I opened the serial monitor by clicking on Tools, Serial Monitor.  The serial monitor displays the output from the Arduino.

Next, I wanted to see what the voltage would be from an external power source, as opposed to the Moteino getting power over the USB cable.  I connected 3.5v power from an adjustable power supply to the VIN pin on the Moteino.  The voltage displayed by the program never changes from 3369 millivolts.  I am not sure if that is because the Moteino was still taking power from the USB or if the built-in power regulator works with any power input.  

I plugged my Arduino Uno into the USB port on the computer and uploaded the script.  It consistently reported 5068 millivolts.  Adding 3.5v power from the external power supply on VIN did not change the readings.

I need to figure out how to get the Arduino or Moteino to send data back to my computer or maybe to a Raspberry Pi without using a USB cable.