Friday, May 27, 2016

Digital To Analog Converter For .NET, C# and VB.NET.

 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.

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:


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:


Sunday, May 22, 2016

Controller Extension Tutorial - Part II

Overview

You can read part I at of this post at Controller Extension Tutorial - Part I.

Beyond 5 volt

Some devices will require 8 or 12 volt, therefore we offer a second extension which allow to plug an external source of power like a 9 volt battery.
Both extension use the transistor 2N3904 which support 12 volt, but the current is still limited to 200 mA per external device controlled.

Beyond the USB 500 milliamp


Note that if each device controlled are 5 volts but require 200 mA each, we cannot use the USB to provide the 600 mA. USB 2.0 only allow to consume 500 mA. In this case we need a 5 volt power adapter providing 600 mA or more.


The extension is configured to use an external source of power by default. But we can revert it back to use the USB power, by de-soldering the connection EX_PWR_ON and solder the connection USB_PWR_ON.

Connecting all the pieces

Now, we have 4 items
  1. The Nusbio board
  2. The Controller Extension
  3. The 9 volts lamp
  4. An external source of power, here a 9 volt battery

Plug the extension into Nusbio and
  • The black wire from the device must be plugged into the G0 pin (Ground 0)
  • The red wire from the device must be plugged into the V0 pin (VCC 0, in short VCC mean +)
  • The black wire or negative from the battery into the EX_POWER_PIN negative pin
  • The red wire or positive from the battery into the EX_POWER_PIN positive pin



Programming

The change of voltage  from 5 to 9 volt does not affect the programming.
The shortest code would look like this and will turn on the device for 500 ms and turn it off for another 500 ms.

var serialNumber = Nusbio.Detect();
if (serialNumber == null) // Detect the first Nusbio available
{
    Console.WriteLine("Nusbio not detected"); return;
}
using (var nusbio = new Nusbio(serialNumber)
{
    while(true) {
        nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(true);
        Thread.Sleep(500);
        nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(false);
        Thread.Sleep(500);
    }
}

To learn more

  Transistor Crash Course For Software Developer - PART I - Transistor As Switch  
  Measuring Current with a Digital Multimeter
  YouTube is your friend, if you want to learn electricity

Saturday, May 21, 2016

Controller Extension Tutorial - Part I

Overview


The Nusbio's Controller Extension allows to turn on and off programmatically using any .NET language up to 3 devices.


In this part I, I will use the simple version of the extension which automatically provide current to power the devices and therefore each device must be 5 volts.
The limit of current per device is 200 milli-amp (mA) because this is the limit of the 2N3904 transistor used by the extension.
Also the total consumption must be less than 500 milli-amp, because this is the USB limit and the devices are really powered by the USB of the computer.

The rule of thumb is that an LED use 20 mA and this lamp has 3 so 60 mA. Simple motor found in printer will use 100 mA. To learn how to measure current consumption, see links at the end of this post.


Where to start?

Pick a 5 volts device like this lamp, even if the device is powered with battery like 3 AA battery (3 x 1.5 ~= 4.5 ~= 5 Volts ) and attach or solder a black wire to the negative and a red wire to the positive. Just follow the current black and red wire currently hooked from the batteries to the device.



a


Now, we have 3 items
  1. The Nusbio board
  2. The Controller Extension
  3. The 5 volts lamp



Plug the extension into Nusbio and
  • The black wire from the device must be plugged into the G0 pin (Ground 0)
  • The red wire from the device must be plugged into the V0 pin (VCC 0, in short VCC mean +)
  • Turning on or off Nusbio GPIO 0 programmatically will power or not the device



Programming

The shortest code would look like this and will turn on the device for 500 ms and turn it off for another 500 ms. The Nusbio .NET library is written in C# compiled as .NET 4.0 and therefore compatible with VB.NET, F#, PowerShell and IronPython.

var serialNumber = Nusbio.Detect();
if (serialNumber == null) // Detect the first Nusbio available
{
    Console.WriteLine("Nusbio not detected"); return;
}
using (var nusbio = new Nusbio(serialNumber)
{
    while(true) {
        nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(true);
        Thread.Sleep(500);
        nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(false);
        Thread.Sleep(500);
    }
}
 

To learn more

  Transistor Crash Course For Software Developer - PART I - Transistor As Switch  
  Measuring Current with a Digital Multimeter
  YouTube is your friend, if you want to learn electricity