søndag den 12. maj 2013

Arduino RGB LED control with C# .NET

This is a simpel exsampel of how to intergrate the use of a RGB led with the Arduino UNO controled by a C# based code.

1. Arduino

The circut
Each of the wires are marked with the same colour as the pin they are controlling.
Pin 3 to Red (Red wire)
Pin 5 to Green (Green wire)
Pin 6 to Blue (Blue wire)
GND to Ground (Black wire)



The code
void setup()
{
  // declare the serial comm at 9600 baud rate
  Serial.begin(9600);

  // output pins
  pinMode(6, OUTPUT); // red
  pinMode(5, OUTPUT); // green
  pinMode(3, OUTPUT); // blue
}

void loop()
{
  // call the returned value from GetFromSerial() function
  switch(GetFromSerial())
  {
  case 'R':
    analogWrite(6, GetFromSerial());
    break;
  case 'G':
    analogWrite(5, GetFromSerial());
    break;
  case 'B':
    analogWrite(3, GetFromSerial());
    break;
  }
}

// read the serial port
int GetFromSerial()
{
  while (Serial.available()<=0) {
  }
  return Serial.read();
}

2. C# .NET

For the sake of this post I made a class which is easy to use, all you have to do is initialize the instance and use the build-in functions to connect and send RGB colour codes to the Arduino. This will only work if associated with the Arduino code above. Feel free to modify the class if necessary!

The code
using System;
using System.IO.Ports;
using System.Linq;

namespace Arduino
{
    class RGBControl
    {
        //Default COM settings
        public   static     string   PortName = "COM3";
        public   static     int      Baudrate = 9600;

        // Create a new SerialPort object with default settings.
        SerialPort serialPort1 = new SerialPort();

        public RGBControl()
        {
            //Connect to Ardunio with defualt COM settings
            ConnectSerialPort(PortName, Baudrate);
        }

        public RGBControl(string _Portname, int _Baudrate)
        {
            //Connect to Ardunio with user specified Portname and Baudrate
            ConnectSerialPort(_Portname, _Baudrate);
        }

        private void ConnectSerialPort(string Portname, int Baudrate) // The connect function
        {
            // Allow the user to set the appropriate properties.
            serialPort1.PortName = Portname;
            serialPort1.BaudRate = Baudrate;

            // Open Port
            serialPort1.Open();
        }

        public void SendRGB(int R, int G, int B)
        {
            if (serialPort1.IsOpen)
            {
                //send information
                serialPort1.Write("R");
                serialPort1.Write(Convert.ToChar(R).ToString());
                serialPort1.Write("G");
                serialPort1.Write(Convert.ToChar(G).ToString());
                serialPort1.Write("B");
                serialPort1.Write(Convert.ToChar(B).ToString());
            }
        }
    }
}
The Class can be downloaded here.
And a sampel project can be downloaded here.

3. Additional information

I used Arduino's own software to program my arduino UNO.
The software can be found here.

I used Visual Studio Ultimate 2012 to program a simpel Windows Form Application.

Feel free to ask questions!

1 kommentar:

  1. www.NusbioMCU.com is full product similar to what you described in your post.

    SvarSlet