Cerbuino Bee Analog Input Question

Hi, I just came across another question for the analog reading in FEZ Cerbuino Bee. In NETMF4.1, we can just use the AnalogIn class which requires the assembly:

GHIElectronics.NETMF.FEZ;
GHIElectronics.NETMF.Hardware;

I have no any difficulty with that.

But I installed the NETMF 4.2 and could not find those assemblies any more. I wonder how canI read the analog signal with Cerbuino Bee? The only class I found is the AnalogInput defined in Microsoft.SPOT.Hardware, but I don’t know the mapping between Cpu.AnalogChannel with Cerbuino on board pins.

Your hints/help will be highly appreciated. :slight_smile:

Prior to NETMF 4.2, Analog and PWM were added to the firmware as an added feature by the hardware vendor. Microsoft has subsequently added these two features to Micro Framework 4.2+. So to answer you question, Microsoft.SPOT.Hardware is the correct AnalogIn assembly to use and it is similar to how the GHI version worked.

@ bbcat - this what you are looking for?

http://www.tinyclr.com/forum/topic?id=7263

And the tutorials will be updated accordingly.

With the upcoming firmware, Cerbuino will have the Arduino-styled headers enumerated for easier access to digital pins, Analog, and PWM plus Gadgeteer functionality will be available as well.

Thanks for the encouraging news.

Just a quick curiosity: how to use AnalogInput class with Cerbuino Bee.

I tried to use: AnalogInput testAI = new AnalogInput (Cpu.AnalogChannel channel); and tried to cast the Cerbuino pin A0 (which is PB1/ADC12_IN9) to Cpu.AnalogChannel without any luck. Is there a way find which pin of Cerbuino Bee maps to the AnalogInput class channel?

Thanks

For the mean time here is the order of channels to pin for the Cerberus/Cerbuino:

// A6,A2,A3,C0,C1,A4,C2,C3,A5,B0,B1

Channel 0 is A6 to Channel 10 is B1.

For the Cerbuino Pin-out Library the Arduino-styled Header will have the A0 - A5 already type casted to the correct channel. As for the Analog on the Gadgeteer sockets, you would either use Gadgeteer code or you will have to manually set the analog channel to the proper channel based on the pin out on the wiki and to what was listed here.

2 Likes

@ Aron -

Is that mapping valid for any STM32 based board ?
I mean is this provided by NETMF porting kit, or done by GHI in the HArdwareProvider ?

Need help, from reviewing this topic I’m trying to write a simple Hello World application on a Cerbuino Bee that reads the value from a non-gadgeteer analog sensor such as a light sensor, temperature sensor, etc. connected to e.g. A2. My simple program below throws an exception when I instantiate AnalogInput testAI. I’m using the channel order noted earlier in this topic.

using Microsoft.SPOT.Hardware;

    void ProgramStarted()
    {
        Debug.Print("Program Started");

        AnalogInput testAI = new AnalogInput(Cpu.AnalogChannel.ANALOG_1); 

        Debug.Print("A2: " + testAI.ReadRaw().ToString());

    }

A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll

try to change " AnalogInput testAI = new AnalogInput(Cpu.AnalogChannel.ANALOG_1) " to:

AnalogInput testAI = new AnalogInput( (Cpu.AnalogChannel) Cpu.AnalogChannel.ANALOG_1);

The pin needs a cast.

Do you have an exception on the “new” or the ReadRaw ?

No

Are you using the Gadgeteer interface? And if so, are you using the sockets or the Arduino-styles headers?

I am actually using a Gadgeteer button connected to socket 3, and then I’m using some of my Arduino Electronic Brick sensors connected to the A0-A5 connection headers. I desire a simple program that illustrates how to read the raw values from the A0-A5 and also how to write to the digital outputs D0-D9. The earlier reply that recommended that I cast did get me past my exceptions e.g.- AnalogInput testAI = new AnalogInput( (Cpu.AnalogChannel) Cpu.AnalogChannel.ANALOG_1); However, when I read the value of the analog input the result never changes even if e.g. the temperature changes. Thanks to everyone for the help so far!

If you are using the Arduino-Style AnalogIn headers, the A1 pin does not necessarily correspond to AnalogIn Channel 1. The channels are relative to how the pins were laid out. It is possible that you are reading one of the other analog pins that have nothing on them.

Under GHI.OSHW.Hardware, there is an enumeration for the proper channel for the Arduino-Styled headers. Here is an example:


AnalogInput testAI = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);

Just add a reference to GHI.OSHW.Hardware and use the using and you can open up the other headers for Cerbuino. Type casting with this method should not be necessary as the enumerations are already typed to Analog channel type.

Thank you Aron for the guidance, your solution worked. I appreciate the excellent support from GHI and everyone else!

Not a problem. We are always happy to help. :slight_smile:

I don’t understand why we don’t need to use
(Cpu.AnalogChannel) for AnalogInput

AnalogInput testAI = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);

or
(Cpu.pin) for digital output

 OutputPort LED;               
            LED = new OutputPort(FEZCerbuino.Pin.Digital.D2,false);

The enumerations provided by GHI have the same values but make it much easier to code than trying to decipher the CPU pin numbers.

Thanks ianlee74.