The Programmator

Read Arduino sensor data from a Raspberry Pi Using I2C and Windows 10 IoT

Introduction

Because there is currently no way to autodiscover an Arduino as an USB peripheral from a Raspberry Pi running Windows 10, I have to resort to I²C communication to get messages from and to the Arduino. I will use a DHT 11 humidity and temperature sensor I had laying around.

For this article to make any sense, you’re expected to have basic knowledge of your Arduino, the Arduino IDE (or deploying via Visual Studio), how to create a very basic Windows Universal Application, and you need to have Windows 10 installed on your Raspberry Pi.

Wiring

I have a Raspberry Pi 2 model B v1.1 and a cheap Arduino. Your pins may vary. Please do not blindly connect pins.

Arduino Uno pins

Arduino Raspberry Pi I2C Wiring

Send sensor data with Arduino

Setting up serial communication on the Arduino is pretty simple. First we need to let the outside world know on which address we will be sending data. Second, include and use the Wire library to set up the communication line on that address. And last, we send the data over the Wire.

Set up I²C Serial Communication on Arduino

As you can see, the declaration of our Arduino project is straightforward. We include the libraries, define the address, I have the DHT11 sensor on digital pin 4, and the latest DHT library requires us to define the type of sensor in the DHTTYPE property.

Next, we instantiate a new dht object, and reserve 2 char arrays of data to be sent.

Read sensor data with Raspberry Pi

Create an empty Windows Universal Application. This application will read and display the temperature coming from the sensor attached to the Arduino!

Create Blank Windows Universal Application

Open the MainPage code behind, and create an async method to initialize the connection. The following block of code creates a connection to the Arduino on the aforementioned address.

Read I2C data from Arduino on Raspberry Pi

! Library reference Gotcha

When auto-resolving missing libraries for I2c namespace objects, I got a series of errors about .winmd. Remove the Windows.Devices.LowLevel… reference, and add a manual reference to “Windows IOT Extensions for the UWP”.

Add Windows IoT Extension

In the timer callback, we read the data and send it to the UI.

Read Arduino sensor data from Raspberry Pi

Finally, set debug target on “Remote Device”, select your Raspberry running Windows 10 and hit Debug!

All source code for this sample (and others) is available on GitHub.

Like what you read? Maybe your friends do too! Share this article:

Leave a Reply

Tags: , , , ,