Friday, May 27, 2016

Analog To Digital Converter For .NET (C#, VB.NET, F#, PowerShell)

Introduction

The world of Raspberry PI and Arduino offers
  • GPIO pins (General Purpose Input Output) 
  • ADC (Analog To Digital converter)
  • DAC (Digital To Analog Converter)
But what you may not know is that you can have these directly programmable to your Windows machine with any .NET languages using the USB device Nusbio.

Videos

Analog Sensors

The analog extension offer 8 10 bits analog to digital converter similar as the one found on an Arduino UNO.
Any 5 or 3 volts analog sensor could be plugged into the extension and their values read from any .NET language.

Vibrator Sensor 


One TMP36 temperature sensor, one light sensor and one motion sensor plugged into the Analog Extension.
The source code to deal with the 3 sensors:

while (nusbio.Loop())
{
    if (halfSeconds.IsTimeOut())
    {
        const int lightSensorAnalogPort = 6;
        const int motionSensorAnalogPort = 2;
        const int temperatureSensorAnalogPort = 0;

        ConsoleEx.WriteLine(0, 2, string.Format("{0,-20}", DateTime.Now, lightSensor.AnalogValue), ConsoleColor.Cyan);

        lightSensor.SetAnalogValue(ad.Read(lightSensorAnalogPort));
        ConsoleEx.WriteLine(0, 4, string.Format("Light Sensor       : {0} (ADValue:{1:000.000}, Volt:{2:000.000})    ",
            lightSensor.CalibratedValue.PadRight(18),
            lightSensor.AnalogValue,
            lightSensor.Voltage), ConsoleColor.Cyan);

        analogTempSensor.SetAnalogValue(ad.Read(temperatureSensorAnalogPort));
        ConsoleEx.WriteLine(0, 6, string.Format("Temperature Sensor : {0:00.00}C, {1:00.00}F     (ADValue:{2:0000}, Volt:{3:000.000})      ",
            analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Celsius),
            analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit),
            analogTempSensor.AnalogValue,
            analogTempSensor.Voltage), ConsoleColor.Cyan);

        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
        var motionType = analogMotionSensor.MotionDetected();
        if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected || motionType == DigitalMotionSensorPIR.MotionDetectedType.None)
        {
            ConsoleEx.Write(0, 8, string.Format("Motion Sensor     : {0,-20} (ADValue:{1:000.000}, Volt:{2:000.000})", motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
        }
    }
} 

This touch sensor only require a digital GPIO, when you finger touch the panel the value at the gpio is a 1.
It could also be plugged into the analog extension.

DAC - Digital To Analog Converter

A DAC offers the ADC reverse function, that is to say starting with a integer range of values for example from 0 to 4097 (12 bits), output voltage between 0 and 5 volt, or 0 or 3.3 volt.

The MCP4725 is a 12 bit, 5 volts digital to analog converter that use the I2C protocol.
Adafruit offer a break out.



Here are 2 videos:


No comments:

Post a Comment