Arduino and Azure IOT, better together

Thought I'd share this with You guys. Just in case You're wondering wether to do this kind of thing or not.


Sharp 2Y0A02 comes from a large family of sensors for distance measuring. There are several versions of the same thing optimized for different needs and use cases. I happened to have this partiular version lying in my junkbox so without too much thought thats what I'll be using for this project. It is optimized for distances between 10cm to 100Cm and it gives out the measurement results as an analog voltage. (Sharp GP2D02 does the same thing but You can use a digital protocol to talk to it just in case You are using some microcontroller that has no analog input pins like Raspberry).

I also have a spare Arduino Leonardo from another experiment so I'll use that . Besides its very cheap and capable.


As You can see from the picture I have USB cable attached my Arduino. It is primarily used to push compiled application to the oboard chip but it can also be utilized by my own software as a serial port.
So I'll be using that USB to talk to my PC-software which does the actual talking to Azure IOT infrastructure. Arduino just takes care of measuring and communicates result to pc.
There are other options :

So in my case I decided to use USB since it also provides +5v to Arduino. 

Arduino code 

The code reads the voltage from our sensor, linearizes it and sends it over serial line to PC. (there will be a link in resources)

I am using a miniprotocol where I prepend the readings with sensorid, in this case "A", then separator ":" , then the actual value and finally a newline as terminator. This because I will attach more sensors later on.
Codewise there is really nothign spectacular going on here. Just plain old plain old.


So the PC is listening to the Arduino and talking to Azure whenever it gets new values. I am also visualizing the received values with a progres bar.The most notable thing here is maybe the pattern of using Action to communicate between receiving thread and ui thread.The actual Azure communication bit is probably very familiar by now and we've been covering it in previous IOT postings in this blog.

As You can see from the code we parse the lines received from Arduino and add the data to send Queue which will automagically sends the events to Azure.
Take a look at the actual file for more details on how to setup serial connection and so forth.

Azure side

On Azure I have setup a Azure Stream Analytics job which stores the received lines into TableStorage and in csv-file for later usage by HDInsight (MS version of Hadoop).
I have blogged earlier about how to setup the Azure infrastructure for capturing events, so take a look at them if You are unsure about these details.
When looking at the data with storage Explorer here is what we see.

Conclusions ,resources and next steps

This article is about data capture and storage which are definitely needed steps in every succesfull IOT solution but the business value usually doesn't lie here but more in the realm of data analysis and machine learning.
My next IOT-articles will concentrate more on the usage of the gathered data since we pretty much have proven that data capture and storage are very close to nobrainers when using Azure PaaS infrastructure.

Arduino code file

C# code file


Comments (3)

  1. Hessu says:

    Nice job! Looking forward on your next blogs about this kind of stuff.

  2. Srikanth Addala says:

    This looks great, I am quit new with this Azure stuff, i will try this now , can you please guide me regarding , arduino sending  data directly to azure with esp8266 wifi ..

    Thank in advance

  3. chris says:

    you can find the tutorial how to directly connect arduino to azure here:…/dn605873.aspx

Skip to main content