Copyright Getting Started with Arduino Wiring for Windows 10 IoT Core Agus Kurniawan 1st Edition, 2016 Copyright 2016 Agus Kurniawan ** Windows 10 IoT Core, Visual Studio and Logo are trademark and copyright from Microsoft ** Raspberry Pi is trademark and copyright from Raspberry Pi, www.raspberrypi.org
Table of Contents Copyright Preface 1. Setting Up Development Environment 1.1 Raspberry Pi 2 1.2 Getting Hardware 1.3 Windows 10 for Raspberry Pi 1.4 Development Tools 1.5 Configuring Windows 10 IoT Core for Raspberry Pi 2 2. Digital I/O 2.1 Getting Started 2.2 Demo 1: Hello World: Blinking LED 2.2.1 Wiring 2.2.2 Writing Program 2.2.2.1 Creating a Project 2.2.2.2 Writing Codes 2.2.3 Configuring Remote Machine 2.2.4 Testing 2.2.5 Debugging 2.3 Demo 2: LED and Pushbutton 2.3.1 Wiring 2.3.2 Writing Program
2.3.3 Configuring Remote Raspberry Pi 2.3.4 Testing 3. Serial Communication 3.1 Serial Port on Windows 10 IoT Core 3.2 Demo Serial Communication 4. Analog I/O 4.1 Getting Started 5. Working with I2C/TWI Protocol 5.1 Getting Started 5.2 Demo: Sensor Device Based I2C 5.2.1 Sensor Device Base I2C 5.2.2 Writing Program 5.2.2.1 Creating a Project 5.2.2.2 Writing Codes 5.2.2.3 Configuring Remote Machine 5.2.3 Testing 6. Working with SPI Protocol 6.1 Getting Started 6.2 Demo: Hello SPI 6.2.1 Hardware Configuration 6.2.2 Writing Program 6.2.3 Testing Source Code
Contact
Preface If you have experiences in Arduino development using Sketch program, now you can do porting your Arduino code to Raspberry Pi 2 with Windows 10 IoT Core. This book was written to help anyone who wants to get started with Arduino Wiring which is targeted to Windows 10 core for Raspberry Pi 2. It describes all the basic elements of the Arduino Wiring with step-by-step approach. Agus Kurniawan Microsoft MVP, 2004-2015 Depok, January 2016
1. Setting Up Development Environment
1.1 Raspberry Pi 2 The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse (source: https://www.raspberrypi.org/help/what-is-a-raspberry-pi/). The Raspberry Pi 2 comes with a much more powerful processor ( four Cortex -A7 cores with up to 900 MHz) and more memory (1GB RAM). The following is technical specification of Raspberry Pi 2 device: ARM 7 Quad Core CPU 1GB RAM 900MHz Board Clock Speed 40 GPIO Pins 4 x USB Ports 4 Pole Stereo Output 1x HDMI Port 1x 0/100 Ethernet 1x Micro SD Card slot You can see Raspberry Pi 2 device with model B on the Figure below.
1.2 Getting Hardware How to get Raspberry Pi 2 device? Officially you can buy it from the official distributor RS, http://uk.rs-online.com/web/generaldisplay.html?id=raspberrypi Element14, http://www.element14.com/raspberrypi You also buy Raspberry Pi peripheral devices for instance, keyboard, mouse, HDMI cable, SD card, USB hub, etc. I tried to look for buying Raspberry Pi 2 device and found that there are another options to buy The Pi Hut, http://thepihut.com Sparkfun, https://www.sparkfun.com/ EXP-Tech, http://www.exp-tech.de/ Cooking-hack, http://www.cooking-hacks.com/ Amazon, http://www.amazon.com Ebay, http://www.ebay.com You also can buy this board at your local electronics stores.
1.3 Windows 10 for Raspberry Pi Microsoft has announced that they release Windows 10 for Raspberry Pi 2, http://blogs.windows.com/buildingapps/2015/02/02/windows-10-coming-to-raspberry-pi- 2/. Now Microsoft already released Windows 10 preview for Raspberry Pi 2, http://blogs.windows.com/buildingapps/2015/04/29/microsoft-brings-windows-10-tomakers/. In this book, we learn how to get started with Windows 10 IoT Core for Raspberry Pi 2 board. In this book, I suppose you have knowledge about Windows 10 IoT Core. I have written e-book, Getting Started with Windows 10 for Raspberry Pi 2, http://blog.aguskurniawan.net/post/getting-started-with-windows-10-for-raspberry- 2.aspx. Make sure you have deployed Windows 10 IoT Core build 10586 or later to work with Arduino Wiring development.
1.4 Development Tools In this book, I use Visual Studio 2015 with OS Windows 10 x64.
1.5 Configuring Windows 10 IoT Core for Raspberry Pi 2 To write a program for Arduino Wiring on Raspberry Pi 2 with Windows 10 IoT Core, you should configure your Windows 10 IoT Core in Direct Memory Mapped Driver. To configure that mode, open a browser and navigate to http://windows10iotcore_ipaddress:8080. The default authentication is username: Administrator and password: p@ssw0rd. Select Devices. You should see Controller Driver configuration.
Change it to Direct Memory Mapped Driver. Then, click Update Driver.
After that, reboot Windows 10 IoT Core. Now your Raspberry Pi 2 with Windows 10 IoT Core is ready for developing.
2. Digital I/O This chapter explains how to work with networking in Raspberry Pi 2.
2.1 Getting Started In this chapter, we work with Digital I/O. In general, digitalread() and digitalwrite() work with Raspberry Pi. Let s start to create projects.
2.2 Demo 1: Hello World: Blinking LED In this section, we learn how to get started with Raspberry Pi programming on Windows 10 IoT Core. We build LED blinking application. We use 3 LEDs to illustrate our case. 2.2.1 Wiring In this section, we learn how to write data using GPIO on Raspberry Pi. We can use 3 LEDs to illustrate our case. Our LEDs are connected to GPIO pins: GPIO 13, GPIO 6, and GPIO 5. LED ground pin is connected to GPIO GND. The following is our demo wiring.
2.2.2 Writing Program In this section, we develop program to access Raspberry Pi GPIO using Visual Studio 2015. 2.2.2.1 Creating a Project Firstly, we create a project. Open Visual Studio 2015. Then, you can create a new project. Select Visual C++-> Windows > Windows IoT Core. Choose Arduino Wiring Application for Windows IoT Core template. Fill LedBlinking on project name. If done, click OK button.
If you don t see this template, you can download and install it on https://visualstudiogallery.msdn.microsoft.com/55b357e1-a533-43ad-82a5- a88ac4b01dec. Then, you can see Sketch program on Visual Studio 2015 code editor.
2.2.2.2 Writing Codes Now we can modify our code on Blinking.ino file. Please open that file and write this code. void setup() { pinmode(gpio13, OUTPUT); pinmode(gpio6, OUTPUT); pinmode(gpio5, OUTPUT); } void loop() { digitalwrite(gpio13, HIGH); digitalwrite(gpio6, LOW); digitalwrite(gpio5, LOW); delay(500); digitalwrite(gpio13, LOW); digitalwrite(gpio6, HIGH); digitalwrite(gpio5, LOW); delay(500); digitalwrite(gpio13, LOW); digitalwrite(gpio6, LOW); digitalwrite(gpio5, HIGH); delay(500); } Save this code. 2.2.3 Configuring Remote Machine You should change target is ARM and Remote Machine.
Then, open project properties. Select Debugging. Fill Raspberry Pi 2 hostname, such as minwinpc. Then, select Universal (Unencrypted Protocol) on Authentication Type. Save the configuration. Now you compile the program. Make sure you don t get any error on compiling. 2.2.4 Testing If your program doesn t get errors on compiling, then you can run this program. Click Remote Machine on Debug menu, see the Figure below.
If success, you should see LEDs are blinking. 2.2.5 Debugging We can debug our program. Just set breakpoints on your code.
After that, you can run debug by click menu Debug. The debugger will stop on your breakpoints.
2.3 Demo 2: LED and Pushbutton We build a simple demo to illustrate I/O on Raspberry Pi GPIO. W need a pushbutton and LED. If you press pushbutton, LED will lighting. Otherwise, it will off. 2.3.1 Wiring Connect your LED into GPIO 13 and pushbutton on GPIO 6. The following is our wiring. 2.3.2 Writing Program Create a project, call PushLed, on Visual Studio 2015. Please follow instructions on section 2.2.2.1. Write this code for PushLed.ino. void setup() { pinmode(gpio13, OUTPUT); pinmode(gpio6, INPUT); }
void loop() { int state = digitalread(gpio6); digitalwrite(gpio13, state); delay(500); } 2.3.3 Configuring Remote Raspberry Pi We must configure our remote Raspberry Pi and project. Please follow instructions on section 2.2.3. 2.3.4 Testing Now you can deploy and execute program into Windows 10 IoT Core on Raspberry Pi 2 board. Press pushbutton to get a response from the program. After pressed, you should see the lighting LED.
3. Serial Communication In this chapter we implement serial port on Raspberry Pi using Arduino Wiring.
3.1 Serial Port on Windows 10 IoT Core Microsoft remove Serial object so we can t use Serial in our program now. Please check it on https://ms-iot.github.io/content/en- US/win10/ArduinoWiringPortingGuide.htm#port_serial. To send data to the program output, Microsoft provide Log(). We can see the message on Output window of Visual Studio 2015.
3.2 Demo Serial Communication For demo, I use a program from section 2.2. We add messages using Log() so we can monitor these message on Output window. Create Arduino Wiring project, called SerialDemo. See section 2.2.2.1. Then, write this code on SerialDemo.ino. void setup() { pinmode(gpio13, OUTPUT); pinmode(gpio6, OUTPUT); pinmode(gpio5, OUTPUT); } void loop() { Log(L"LED 1 ON \n"); digitalwrite(gpio13, HIGH); digitalwrite(gpio6, LOW); digitalwrite(gpio5, LOW); delay(500); Log(L"LED 2 ON \n"); digitalwrite(gpio13, LOW); digitalwrite(gpio6, HIGH); digitalwrite(gpio5, LOW); delay(500); Log(L"LED 3 ON \n"); digitalwrite(gpio13, LOW); digitalwrite(gpio6, LOW); digitalwrite(gpio5, HIGH); delay(500); } Configure your project to Remote machine for Raspberry Pi. See section 2.2.3. After running, you can see message on Output window from Visual Studio.
4. Analog I/O This chapter explains how to work with Analog I/O in Raspberry Pi 2.
4.1 Getting Started Unfortunately, Raspberry Pi 2 doesn t have analog I/O so technically we can t use analogread() and analogwrite() in our program. Alternatively, we can access Analog sensors via SPI and I2C. You can also use ICs such as MCP3008, which support for converting from analog to SPI or I2C. A sample of sensor module-based I2C can be read on chapter 5.
5. Working with I2C/TWI Protocol This chapter explains how to work with I2C on Raspberry Pi 2 board and write program to access I2C.
5.1 Getting Started The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early 80s to allow easy communication between components which reside on the same circuit board. TWI stands for Two Wire Interface and for most marts this bus is identical to I²C. The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues related to I²C. I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). The following is I2C bus on Raspberry Pi 2 board. To connect device via I2C, you can do the following connection.
You should define which to be master and slave. We also can define 1 master and two slave devices. There are many devices which use I2C to communicate to other. The following is a list of sample device with I2C enabled: EEPROM IC with I2C support ADC/DAC with I2C support, for instance, http://www.adafruit.com/product/935 Real Time Clock (RTC), http://www.adafruit.com/products/264 LCD+Keypad module with I2C support, for instance, http://www.adafruit.com/product/1109 For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices. You can find it on the following online store: Amazon, http://www.amazon.com/pcf8591-converter-module-digital- Conversion/dp/B00BXX4UWC/ ebay, http://www.ebay.com Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-toanalog-converter-module-w-dupont-cable-deep-blue-336384 Aliexpress, http://www.aliexpress.com/ In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon, http://www.amazon.com/waveshare-pcf8591t-converter-evaluation- Development/dp/B00KM6X2OI/. This module use PCF8591 IC and you can read the datasheet on the following URLs. http://www.electrodragon.com/w/images/e/ed/pcf8591.pdf http://www.nxp.com/documents/data_sheet/pcf8591.pdf On Windows 10 IoT Core for Raspberry Pi 2, we can access I2C via Arduino Wiring
program.
5.2 Demo: Sensor Device Based I2C The objective of this section is to illustrate how to read data on I2C bus. We use PCF8591 AD/DA converter with sensor devices as I2C external source. You can read this module on this link, http://www.electrodragon.com/w/images/8/89/pcf8591_adc_dac_adda_analog_dig. In our scenario, we will read three sensor devices in PCF8591 AD/DA modules via I2C. Let s start!. 5.2.1 Sensor Device Base I2C Now you can connect your PCF8591 AD/DA to Raspberry Pi 2 board. The following is demo wiring : Module SCL > Raspberry Pi 2 board SCL Module SDA > Raspberry Pi 2 board SDA Module VCC > Raspberry Pi 2 board +3.3V Module GND > Raspberry Pi 2 board GND The following is a sample of hardware implementation. 5.2.2 Writing Program
In this section, we build a program to access sensor via I2C port. 5.2.2.1 Creating a Project Create a new project on Visual Studio 2015, called SensorI2cDemo. Follow the instructions on section 2.2.2.1 5.2.2.2 Writing Codes Now we modify our code on SensorI2CDemo.ino file. Please open that file and write this code. #include <Wire.h> #define PCF8591 (0x90 >> 1) // I2C bus address #define PCF8591_ADC_CH0 0x00 // thermistor #define PCF8591_ADC_CH1 0x01 // photo-voltaic cell #define PCF8591_ADC_CH2 0x02 #define PCF8591_ADC_CH3 0x03 // potentiometer ULONG ADC1, ADC2, ADC3; void setup() { // put your setup code here, to run once: Wire.begin(); } void loop() { // read thermistor Wire.beginTransmission(PCF8591); Wire.write((byte)PCF8591_ADC_CH0); Wire.endTransmission(); delay(500); Wire.requestFrom(PCF8591, 2); ADC1 = Wire.read(); ADC1 = Wire.read(); Log(L"Thermistor=%d \n", ADC1); // read photo-voltaic cell Wire.beginTransmission(PCF8591); Wire.write(PCF8591_ADC_CH1); Wire.endTransmission(); delay(500); Wire.requestFrom(PCF8591, 2);
ADC2 = Wire.read(); ADC2 = Wire.read(); Log(L"Photo-voltaic cell=%d \n", ADC2); // potentiometer Wire.beginTransmission(PCF8591); Wire.write(PCF8591_ADC_CH3); Wire.endTransmission(); delay(500); Wire.requestFrom(PCF8591, 2); ADC3 = Wire.read(); ADC3 = Wire.read(); Log(L"potentiometer=%d \n", ADC3); } delay(1000); You can see that we obtain sensor data from the second byte from sensor device. Save this code. 5.2.2.3 Configuring Remote Machine Please configure your remote machine and project based on instruction from section 2.2.3 5.2.3 Testing Now you can compile and upload the program to the board in debugging mode. The following is a sample output on debug output
6. Working with SPI Protocol This chapter explains how to work SPI on Raspberry Pi 2 board and how to access SPI using Arduino Wiring.
6.1 Getting Started The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one or more slave peripheral integrated circuits (ICs) to a single master SPI device; usually a microcontroller or microprocessor of some sort. Raspberry Pi 2 has two SPI ports. SPI in Raspberry can be defined on the following pins: SPI0: SPI1: MOSI (GPIO 19) MISO (GPIO 21) SCLK (GPIO 23) CS0 (GPIO 24) CS1 (GPIO 26) MOSI (GPIO 38) MISO (GPIO 35) SCLK (GPIO 40) CS0 (GPIO 11) You can see these pins on the Raspberry Pi GPIO pins, shown in Figure below.
Raspberry Pi works as SPI Master. If you want to connect to SPI Slave Devices, you can connect with the following configuration. (image source: http://en.wikipedia.org/wiki/serial_peripheral_interface_bus)
6.2 Demo: Hello SPI The objective of the demo is to get started how to work with SPI on Raspberry Pi 2 board using Arduino Wiring. In this scenario, we apply SPI loopback. The following is our scenario: Send one byte data to SPI0 Receive one byte from SPI0 Because we build SPI loopback, we will receive what we sent. Let s start. 6.2.1 Hardware Configuration Hardware configuration is easy. You just connect MOSI and MISO pins with each other. In this demo, I used SPI0 on Raspberry Pi 2 board so I connect MOSI (GPIO 19) pin to MISO (GPIO 21) pin through a jumper cable. The following is a sample of hardware configuration.
6.2.2 Writing Program Create a new project using Visual Studio 2015, called SPILoopback. Please read section 2.2.2. Then, modify *.ino file with the following code. #include <SPI.h> ULONG senddata, recvdata; // source: // http://forum.arduino.cc/index.php?topic=197633.0 ULONG randomdigit() { unsigned long t = micros(); byte r = (t % 10) + 1; for (byte i = 1; i <= 4; i++) { t /= 10; r *= ((t % 10) + 1); r %= 11; } return (r - 1); } void setup() { SPI.begin(); } void loop() { senddata = randomdigit(); recvdata = SPI.transfer(sendData); } Log(L"Send= %d \n", senddata); Log(L"Recv= %d \n", recvdata); delay(800); Save this code. Try to configure your project and remote machine based on section 2.2.3. 6.2.3 Testing Compile and upload your program to remote Raspberry Pi 2 board. You should see sent and received data on Output console. A sample output can be seen in Figure below.
Source Code Source code can be downloaded on http://www.aguskurniawan.net/book/codes_arwir.zip.
Contact If you have question related to this book, please contact me at aguskur@hotmail.com. My blog: http://blog.aguskurniawan.net.