No solution to 'analogRead' was not declared in this scope

I can find references to a similar error all over the place but none of the solutions seem to fix my code. What I got is a ccp and h file that contain a couple of functions to run an ACS712. The code work fine with I had it combined into my main class. Once I move it to the ccp file I get the error "error: 'analogRead' was not declared in this scope". I've look at the code for days and can't figure it out. Any ideas. Thanks, in advance. I include the code below.


#ifndef ACS712_H
#define ACS712_H
//#include <Arduino.h>

int currentPin, tripPin;
int sensitivity = 66;
int adcValue= 0;
int offsetVoltage = 2500;
double adcVoltage = 0;
double currentValue = 0;

double amp_trip_value = 15;  // The amp you want the system to trip at. 

void create_amp_meter(int); 
double read_amp_meter(); 
bool amp_trip(); 



#include "ACS712.h"

/* Create the amp meter. 
void create_amp_meter(int amp_pin)
  currentPin = amp_pin; 


/* Read the amp meter and do the math to get the amp value. 
double read_amp_meter()
  adcValue = analogRead(currentPin);
  adcVoltage = (adcValue / 1024.0) * 5000;
  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
  return currentValue; 

/* Check to see if the trip point has been reached.  This function is 
 * ment to trip if the amp over shoot the amp_trip value. This
 * is to prevent burning up the heating coil if something goes wrong. 
bool amp_trip()
  double reading = read_amp_meter(); 
  if (reading > amp_trip_value)
    return true; 
  return false; 
//#include <Arduino.h>

Why is this #include commented out ?

1 Like

Could be incorrect, because the "2500"mV sensor output at zero Amp is supply dependent.

Same here. Supply could be anything between 4600 and 5200.

Why not forget all the voltage crap,
and convert this ratiometric sensor directly from A/D value to current.
Could be one simple line of code.

current = (analogRead(sensorPin) - offset) * 0.0732;

With "current" declared as float, and "offset" an int value of about 512 (half of the A/D range).
The magic number comes from 30A divided by the A/D swing of the sensor ( ±410).

It was something that was recommended in other places for this problem. It did not fix the problem.

That is not a bad idea and I'll give it a try once the analogRead problem is solved. Without analogRead I can't even test it.

You didn't specify which pins.

const byte currentPin = A0;


It is needed. Maybe there is some other problem as well, but you'll never get past that error if you don't have an #include directive for the header file that declares the function.

You don't need to do that in .ino files because the Arduino sketch preprocessor automatically adds the #include <Arduino.h> to any sketch that is missing it, but it doesn't mess with .h and .cpp files like that, so if you need any of the standard core Arduino API, you must add the necessary #include directive yourself.

1 Like

currentPin is set when the constructor is called. It is the create_amp_meter(int) function.

After including it I get the error "multiple definition of `amp_trip_value'" for every variable that was declared.

I must be blind, because I don't see an actual pin number anywhere in the code you posted.

float amp_trip_value = 15.0;

What about 15Amp flowing in the other direction (-15A).

It is not actually there. The program in the post is a sub-class. I'm not sure if it is common to use this in Ardunio but it is very common in Java. It basically a program that can be written once and then reused very easily. The main program would have your setup() and loop() functions and you would call the constructor in the setup() function. Something like

void setup()

Then in your loop() function, you would just need to call the read_amp_meter() to read the value. Something like

     double d = read_amp_meter(); 

Sorry, I actually do have a tool to see what's not there. A crystal ball.
But mine is out for repair.

Understand. Thanks for your help.

A quick update. I looked back at some of my other ccp and h files. I did not seem to need to include the #include <Arduino.h> there to call Arduino functions. It would make more sense to me if that it should be there but...

Please share an example.

It would definitely be needed and there. Perhaps inside another file that was #include(d).

My mistake it was there. The lack of sleep working on this is starting to get to me. So I just struggling with the error "multiple definition of `amp_trip_value'" for every variable that was declared. After including it.

Why, and where do you use it.

The compiler error message will give details on where it's finding multiple definitions.

To understand the reason for this, visualize that anywhere you have this in your code

#include "ACS712.h"

The entire contents of the file ACS712.h are pasted.

So in your .ino file you have this:

double amp_trip_value = 15;  // The amp you want the system to trip at. 

and in ACS712.cpp you have this:

double amp_trip_value = 15;  // The amp you want the system to trip at. 

There are your multiple definitions.