Issue with analog input ports on AMP 2.6

I am attempting modify the ardupCopter code and use "analogRead( )" to interpret an external input.  However, when compiling I receive the following error: "analogRead was not declared in this scope."  I tried to include the Arduino.h file in the library (I am under the assumption that Arduino.h contains the instructions for the analogRead function).  Any suggestions on how to include analogRead and other common arduino functions with the arduCopter code?  The following is a brief description of what I am trying to accomplish.  Any suggestion or different approaches are appreciated.

Currently I have a raspberry pi w/ camera module performing some color and shape recognition.  When it detects the desired object, I have the pi output 3.3V at one of its ports.  I am trying to connect this port to one of the analog input ports on the APM 2.6.  The copter will be flying a predetermine route to scan an area for the object.  When the object is detected I am having the high input signal the copter to switch from AUTO to LOITER flight mode.

Any help is greatly appreciated.


You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • I had to include an Arduino.h file in order to even get it to compile.  What I can't figure is that the Arduino.h file is empty.  Go figure.

  • Did you figure this out?  Look in Libraries AP_HAL and AP_HAL_AVR then search for board_vcc_analog_source in system.pde.  That should get you started. It's all done in HAL now and I do not fully understand all that for sure.  If you figure it out, let me know with a sample.  I tried this: It compiled but not sure it reads the volts since I could not get to the pins on my APM (in a copter)

    AP_HAL::AnalogSource* testain;
    testain = hal.analogin->channel(0); // read A0 ??
    float testain1 = testain->read_average();
    cliSerial->printf_P(PSTR("Batery volts = %4.4f \n"),testain1);

    HAL sure complicated things for those with only APM.

    Perhaps someone else can chime in.  Example of how to read An  (analog in A0, A1, ,,,)

    • I tested your code...

      AP_HAL::AnalogSource* testain;
      testain = hal.analogin->channel(1); // read A1 pete
      float testain1 = testain->read_average();
      //cliSerial->printf_P(PSTR("Batery volts = %4.4f \n"),testain1);
      gcs_send_text_fmt(PSTR("\n Batery volts = %4.4f \n"),testain1);

      and got this error...

      Initialising APM...

      XError: AP_HAL_AVR::AVRAnalogIn out of channels

      the hal board is sure a pain.


      • Hum,  I put that code in the version 3.1.3 (download is marked 3.1.2) in  arduCopter.pde, in setup(), right after init_ardupilot();  and it compiled fine and when upload, then use serial monitor to connect - the message shows up but the value testain1 is always 0.00    You are correct - a pain.  

        well, Matt has a workaround for now

    • I was able to come up with a solution to the problem but it is definitely not a conventional method.  Since A0 is already set up to read the sonar senor if enabled, I enabled the pin and used this code segment:

      int16_t temp_alt = sonar->read();
      if(temp_alt > 200) {

      I chose 200 (cm) after using the test terminal in MP and seeing that the output was ~31 cm when the RPi was connected to pin A0 outputting a low and ~257 cm when the RPi found the object and was outputting a high (3.3V).

      On a separate note, would you be able in assisting me in compiling and building any version (currently attempting to use v1.2.57) of Mission Planner using Visual Studio.  Once the the copter is loitering over the target I need to extract its GPS location from the tlog file and export it to a file that can be used while the copter is still in flight and connected to MP.  I believe in order to do this I need to modify the source code for MP.  Thanks.


      • When I tested this code

        int16_t temp_alt = sonar->read();
        if(temp_alt > 200) {

        I got this error:

         error: base operand of '->' has non-pointer type 'AP_RangeFinder_analog'

        the hall board kills me again.

This reply was deleted.


Shivchand Jaysaval liked Shivchand Jaysaval's profile
Aug 25