2
\$\begingroup\$

I have an Arduino ProMicro 5V 16MHz board from sparkfun.

I have a 3Cell LiPo powering the entire thing (via the relevant regulator bringing it down to 5V). This is part of a TriCopter. I tap the 12V positive pin through a voltage devider into pin A1 (if the USB is pointing up, and the pins away from you, this is the 6th pin from thje botttom right).

How do I refer to this pin in my program? A1 is: static const uint8_t A1 = 19U. This reads 0V from my battery.

My code is as follows:

/*
 * Battery Sensor
 * Battery Voltage monitor 
 */
const float ReferenceVoltage = 5;   //default reference on a 5-volt board
const float R1 = 54350;//49200;             //55K2 Ohm resister
const float R2 = 32600;//31000;             //32K6 Ohm resister
const float ResistorFactor = 1023.0*(R2/(R1+R2));
const int BatteryPin = A1;          // V(out) of Voltage devider circuit is attached to analog pin A1


/// <summary>
/// Reads the current Battery voltage
/// </sumary>
void ReadBatteryVoltage()
{
    int Value = analogRead(BatteryPin); //read the value from the Analog Pin
    //Serial.println(Value);
    float BatteryVoltage = (Value/ResistorFactor)*ReferenceVoltage; //Calculate the ratio
}

I know this is an electronics forum and I'm asking a programming question, but this is electronics. I would apreciate any pointers. This is driving me mad.

update: Thanks to PeterJ for helping me confirm what I knew about which Pin to read from and also for the improved syntax. The problem was however in line 8 below:

1: /// <summary>
2: /// Reads the current Battery voltage
3: /// </sumary>
4: void ReadBatteryVoltage()
5: {
6:  int Value = analogRead(BATTERY_PIN);    //read the value from the Analog Pin
7:  //Serial.println(Value);
8:  BatteryVoltage = (Value/ResistorFactor)*ReferenceVoltage;   //Calculate the ratio
9: }

That local variable was the one getting populated with the Battery voltage being read from the BATTERY_PIN leaving the similarly named global variable, elsewhere in my application at it's default state of 0.0;

I just removed the type declaration which made it use the global variable as it should and all worked fine.

This is the type of thing that drive programmers crazy. Hardware is hard...pfff! hardware is hard because most hardware now requires software. ;-)... which of-course is created badly by wetware.

\$\endgroup\$
1
\$\begingroup\$

There may be other problems, but according to the Arduino analogRead documentation

Syntax

analogRead(pin)

Parameters

pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

So as A1 is defined as 19U, you should change:

const int BatteryPin = A1;

To the following:

const int BatteryPin = 1;

Personally I would also take a look at using preprocessor #define statements in those cases rather than constants, which may consume additional memory depending on compiler optimizations, so you could use the following:

#define BatteryPin 1

Although by convention for preprocessor defines it would normally be:

#define BATTERY_PIN 1
\$\endgroup\$
  • \$\begingroup\$ When I change "const int BatteryPin = 1" to "#define BATTERY_PIN 1" I get an error on the line where I do the analogRead saying "Error: Expected a ')'"?!? \$\endgroup\$ – Gineer Feb 6 '13 at 6:10
  • 1
    \$\begingroup\$ I don't suppose out of habit you've put a semicolon on the end of line? If so the result would end up as analogRead(1;) \$\endgroup\$ – PeterJ Feb 6 '13 at 6:11
  • \$\begingroup\$ I was about to come and post exactly that. Removed the semicolon and everything compiles. \$\endgroup\$ – Gineer Feb 6 '13 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.