Cobra II plain Analog Input without socket?

I’m converting old Cobra I code over to my new Cobra II. I have two analog sensor inputs that read low level voltage values. (1-2v).

The code I see in the Gadgeteer is specifically designed to work with the sockets; however I want to use a single pin, such as P0.12 and P0.13

P0.12 is listed on the board as AD6 and P0.13 as AD7

so I use the Microsoft.SPOT AnalogInput rather than the Gadgeteer AnalogInput as follows:

var aInput = new AnalogInput(Cpu.AnalogChannel.ANALOG_6,3.3,0,12);

This methodology always results in a value of Zero.

I’ve tried this with several different Analog enabled pins with the same result.

I was able to get something working with the following code however I feel I’m breaking methodology by creating a new socket.

I do get valid results reading voltage from the pin I’m just concerned that my methodology appears to be a bit off because I’m creating a socket with only one active pin, which isn’t compliant – to say nothing of the fact that the socket itself is virtual and doesn’t exist.

Here’s the code that works:


            var ns = GT.Socket.SocketInterfaces.CreateNumberedSocket(21);
            ns.SupportedTypes = new char[] {'A'};
            ns.CpuPins[3] = G120.Pin.P1_31;
            GT.Socket.SocketInterfaces.SetAnalogInputFactors(ns, 3.3, 0, 12);
            ns.AnalogInput3 = Cpu.AnalogChannel.ANALOG_5;
            GT.Socket.SocketInterfaces.RegisterSocket(ns);

            var s1 = GT.Socket.GetSocket(21, true, null, "MULT2");
            a1 = new GT.Interfaces.AnalogInput(s1, GT.Socket.Pin.Three, null);

So the difference that sprung out at me is the second approach uses ANALOG_5 as the channel.

Were you sure you qualified the definition of AnalogInput in the first example? Not sure that this would give you the behavior you saw ?

P1.31 is Analog_5
P1.30 is Analog_4
P0.12 is Analog_5
P0.13 is Analog_7

Here’s how I tested it…


Microsoft.SPOT.Hardware.AnalogInput ain;
ain = new Microsoft.SPOT.Hardware.AnalogInput (Cpu.AnalogChannel.ANALOG_5,3.3,0,12);
for (int i=0;i<100;i++)
                Debug.Print("val=" + ain.ReadRaw().ToString());


Each time I used a different analog channel, I could see the value change as I varied a variable resistor.