Electronics, music, electronic music! general geekery


Make C# talk to Arduino!

I recently wrote a short tutorial about connecting an Arduino to a Lab of Things hub. If you're not familiar with Lab of Things (LoT), it's a platform for research using connected devices (think home automation and energy usage for example). Using LoT, a researcher can deploy and monitor sensors/devices in hundreds of locations. Because of what it is designed to do, LoT is fairly complex. In an attempt to keep my head from exploding I created a very simple C# console app called Cereal to test serial connections outside of the LoT platform code. In this tutorial we'll walk through a much simpler example to introduce the concepts without too much noise. In the next installment we'll cover some useful patterns for sending commands and handling sensor data. For now however we'll keep it simple, so let's get C# talking to Arduino.

Note: To complete this exercise you'll need Visual Studio (or other C# compatible IDE), an Arduino and the Arduino IDE.

The very first version of Cereal was as bare-bones simple as possible. To run SuperSimpleCereal, just create a new C# console app and paste in this code:

To accompany the super simple example, I adapted an equally simple Arduino sketch from the LoT Arduino example:

If you upload the sketch to your Arduino, then run the SuperSimpleCereal console app, you'll see the device ID string (as defined in the sketch) display in the console window. So now you can see just how simple it is to send and receive messages programmatically. Square brackets are used to delimit messages coming from the Arduino, which makes it easier to parse. You can change the console app to send "[L]" and see the other message. I like these examples because they're short and sweet, but there is clearly much more you can do (I'm thinking sensors). Stay tuned for the next installment where we'll take a closer look at Cereal.