Main Site Documentation

Analog Channels on the Hydra


#1

Good Monday-Morning! I’m working on a project that uses two SEPARATE analog inputs on the Hydra. I looked at the analog input tutorial (https://www.ghielectronics.com/docs/8/analog-inputs), and developed the following code to get data from all 6 pins:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using System.Text;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using Microsoft.SPOT.Hardware;
using GHI.Hardware.FEZHydra;

namespace reading_io_straight_from_hydra
{
    public partial class Program
    {
        void ProgramStarted()
        {
            AnalogInput Analog0 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_0);
            double AnalogReading0 = 0;

            AnalogInput Analog1 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_1);
            double AnalogReading1 = 0;

            AnalogInput Analog2 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_2);
            double AnalogReading2 = 0;

            AnalogInput Analog3 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_3);
            double AnalogReading3 = 0;

      [quote]      A[/quote]nalogInput Analog4 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_4);
            double AnalogReading4 = 0;

            AnalogInput Analog5 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_5);
            double AnalogReading5 = 0;

            while (true)
                {
                AnalogReading0 = Analog0.Read();
                Debug.Print("A0 = " + AnalogReading0.ToString());

                AnalogReading1 = Analog1.Read();
                Debug.Print("A1 = " + AnalogReading1.ToString());

                AnalogReading2 = Analog2.Read();
                Debug.Print("A2 = " + AnalogReading2.ToString());

                AnalogReading3 = Analog3.Read();
                Debug.Print("A3 = " + AnalogReading3.ToString());

                AnalogReading4 = Analog4.Read();
                Debug.Print("A4 = " + AnalogReading4.ToString());

                AnalogReading5 = Analog5.Read();
                Debug.Print("A5 = " + AnalogReading5.ToString());

                Thread.Sleep(250);
                }
         }


    }
}

Currently only one pin is hooked up to a pot; the rest are floating. But when I play with the pot, ALL the values change as seen in the debugging window in the pic!

Why is this, and how do I change it? Thanks in advance!


#2

It is good practice to ground all the unused analog inputs. Floating inputs usually give garbage results, and can impact other inputs.


#3

Internally, and this is in most chips, there is only one analog circuit and all analog pins are multiplexed to the one analog circuitry. Like mike said, it is better to connect all analog pins.


#4

Awesome, thanks! Seems to be working better; I was afraid I had a short somewhere and then I’d hook up two different voltages to the same pin and then I wouldn’t have a Hydra anymore. But the multiplexing makes sense to me!

Thanks! :slight_smile:


#5

By the way, a while(true) loop in ProgramStarted is the best way to get a Gadgeteer program to not work as expected. I suspect you were only doing that for testing, but don’t do it in a real app ! http://blogs.msdn.com/b/net_gadgeteer/archive/2011/12/19/why-not-while-true.aspx