Getting Started With Arduino and Go by Agus Kurniawan
Getting Started With Arduino and Go by Agus Kurniawan
Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 Arduino
1.1.1 Arduino Uno
1.1.2 Arduino Leonardo
1.1.3 Arduino Mega 2560
1.1.4 Arduino Due
1.2 Electronic Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit
1.3 Go
1.4 Arduino Software
1.5 Testing
2. Hello World: Arduino and Go
2.1 Arduino World
2.1.1 Arduino Hardware Driver on Windows 8/8.1
2.1.2 Simple Testing
2.2 Arduino and Go
2.3 Testing Serial Port using Go
2.4 Testing for Arduino and Go
3. Exploring Go Packages for Arduino
3.1 Getting Started
3.2 Gobot
3.3 go-firmata
3.4 Reading Digital Input
3.4.1 gobot
3.4.2 go-firmata
4. Analog Sensor
4.1 Sensor Devices
4.2 Reading Sensor
4.3 Running Program
5. RGB LED
5.1 RGB LED
5.1.1 Arduino Analog output (PWM)
5.1.2 Controlling RGB LED Color
5.2 Arduino Implementation
5.3 Go Implementation
Source Code
Contact
Preface
This book was written to help anyone want to get started with Arduino and Go. It
describes the basic elements of the integration of Arduino and Go.
Agus Kurniawan
Depok, March 2015
1.1 Arduino
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use
hardware and software. This board uses Atmel microcontroller series. There are many
Arduino hardware models that you can use. Further information about Arduino products,
you can visit on website http://arduino.cc/en/ .
You must one Arduino hardware to follow practices in this book. I recommend to obtain
one of the following Arduino hardware:
Arduino Uno
Arduino Leonardo
Arduino Mega 2560
Arduino Due
You can buy this product on your local electronic store. You also can order it by online.
Find it on http://arduino.cc/en/Main/Buy. The following is the list of Arduino store you
can buy
Arduino store, http://store.arduino.cc/
Amazon, http://www.amazon.com
Cooking-hacks, http://www.cooking-hacks.com/index.php/shop/arduino.html
RS Components, http://www.rs-components.com
Element 14, http://www.element14.com
EXP-Tech, http://www.exp-tech.de
Because Arduino is an open-source hardware, people can build it. Its called Arduino
compatible. Generally its sold in low prices.
http://arduino.cc/en/Main/ArduinoBoardMega2560 .
1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino
Mega.
1.3 Go
The official web of Go could be found on https://golang.org/. What is Go? Based on
information from website, we could know what it is. Go is an open source programming
language that makes it easy to build simple, reliable, and efficient software.
Installation of Go application is easy. For Windows and Mac Platform, you download
setup file from Go website, http://golang.org/doc/install. Run it and follow installation
commands.
The next step is to configure GOROOT path. For Windows platform, you can add
GOROOT variable on Environment Variables. For Mac/Linux, you can it on your bash
profile.
For Windows platform, you can add GO installation path, for instance my Go installation
path is c:/go/bin, into PATH variable on Environment Variables.
After configured, you can verify Go version by typing this command on your Terminal or
CMD for Windows.
$ go version
In this book, I dont explore all programming language for Go. You can read it on several
book or website. I have already written a book about Go Programming by Example. You
can get this book on this link, http://blog.aguskurniawan.net/post/Go-Programming-byExample.aspx .
For Windows platform, you can download setup file and install it.
The following is a screenshot of Arduino software on Ubuntu platform.
If you run Arduino software on Windows platform, you should configure arduino.exe
running as Administrator. You can change it by editing file property. Click Compatibility
and then checked Run this program as an administrator.
1.5 Testing
For testing, I used Arduino Uno R3 and Arduino Mega 2560 on Ubuntu and Windows 8.1
platforms.
This chapter explains how to work with Arduino and Go for getting started.
Then you execute Arduino software. In general it will detect Arduino hardware include
Arduino type and model.
You can update this device driver by navigating hardware driver on the driver folder of
Arduino software installation folder, for instance E:\arduino-1.0.2\drivers .
Sometime you may get a problem especially for x64 edition about digital signature of
Arduino driver, shown in Figure below.
Configure to ignore the digital signature settings on your Windows. Normally you can do
it by pressing F8 key on restarting Windows. Then choose Disable Driver signature
Enforcement.
For Windows 8, you can do by clicking Restart menu and pressing SHIFT key. Select
Troubleshoot. Then select Advanced options.
On Advanced options display, select Startup settings, shown in Figure below.
Click Restart button. Then Windows 8 will restart. After that, select Disable driver
signature enforcement.
On Arduino software, Click File -> Examples -> 01.Basics -> Blink.
Compile and upload Blink app to Arduino hardware. If success, you can see the LED will
be on/off every second.
In this section, I just show you how to use GoSerial to communicate with serial port.
$ cd serialtest
Note: change your serial port. In this section, my Arduino is connected to COM6 in
Windows platform.
Because we use external package from Github, you must install git runtime.
To install Go library from Github, you must instal git, http://git-scm.com/ . Download it
based on your platform. If you use Windows platform, dont forget to set it on PATH on
Environment Variables.
The next step is to configure GOPATH. It represents our workspace path, for instance, my
workspace path is D:\PE_PRESS\eBook\arduino_go\codes. In Linux/Mac, you can
define your own workspace path in under your account home path, for instance. Further
information, you can read it on https://golang.org/doc/code.html .
If you use Linux/Mac, you can define GOPATH using export command or you add it on
your profile file.
$ mkdir $HOME/go
$ export GOPATH=$HOME/go
If you use Windows platform, you open Environment Variables. You can open it from
Advanced system settings. Then, click Environment Variables button. Add GOPATH
on your user and System variables.
Now we can install goserial library from Github. Type the following command.
$ go get github.com/tarm/goserial
Explanation
On setup(), we activate serial port on 9600 baud rate and LED on pin 13
On loop(), we write HIGH on pin 13 and then send a message LEAD is HIGH\n to
serial port
We also do it again. It writes LOW value and sends a message LEAD is LOW\n to
serial port
Save this code as blinked. Compile and upload this code to Arduino hardware.
Make sure you already configure Arduino board and serial port of Arduino board.
You can compile by clicking icon checked and deploy to Arduino by clicking icon arrow.
You can check the serial port response using Serial Monitor. Click menu Tools -> Serial
Monitor. You will see messages from Arduino hardware.
On the next step, we create Go app. Basically we create a Go app to listen incoming
messages from serial port. Create a project, called serialdemo, by creating folder,
serialdemo.
$ mkdir serialdemo
$ cd serialdemo
You can see the incoming message from serial port of Arduino. To exit from program, you
press ENTER from your PC keyboard.
In this chapter Im going to explain how to use external Go packages to access Arduino
directly.
To use Arduino with Firmata protocol on Arduino, we must load Firmata protocol module
on Arduino. Firstly we must load Firmata protocol on your Arduino board. Open Arduino
software and click menu File -> Examples -> Firmata ->StandardFirmata .
Now you can compile and deploy Firmata Protocol code into your Arduino board. Dont
forget to select your Arduino board model and serial port.
The next step is to build a simple app, blinking led, using Go with Gobot and go-firmata
packages.
3.2 Gobot
Gobot is a framework for robotics, physical computing, and the Internet of Things, written
in the Go programming language. Further information about gobot, you can visit the
official website on http://gobot.io/.
In this section, we try to access Arduino via gobot. To install gobot package, you must
install the following runtime:
git, http://git-scm.com/
mercurial, http://mercurial.selenic.com/wiki/Download
Now you can install gobot package with the following command
$ go get -d -u github.com/hybridgroup/gobot/...
You also install Firmata for Gobot package. Type this command.
For illustration, we build a simple app, blinking led. We use LED on the board, LED on
pin 13.
Now we create project called ledgobot by creating a folder, ledgobot.
$ mkdir ledgobot
$ cd ledgobot
$ go run main.go
3.3 go-firmata
The second Firmata package for Go is go-firmata. You can read it
on https://github.com/kraman/go-firmata . You can install this package by typing this
command.
$ go get github.com/kraman/go-firmata
For illustration, we build a simple app, blinking led. We use LED on the board, LED on
pin 13.
Now we create project called firmataclient by creating a folder, firmataclient.
$ mkdir firmataclient
$ cd firmataclient
3.4.1 gobot
You create project called gobutton by creating a folder, gobutton.
$ mkdir gobutton
$ cd gobutton
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{button,led},
work,
))
gbot.Start()
}
3.4.2 go-firmata
Create project called firmatabutton by creating a folder, firmatabutton.
$ mkdir firmatabutton
$ cd firmatabutton
import (
"fmt"
"time"
"github.com/kraman/go-firmata"
)
func main() {
board, err := firmata.NewClient("COM6",57600)
if err!=nil {
panic(err)
}
defer board.Close()
// LED on digital pin 9
board.SetPinMode(9,firmata.Output)
// button on digital pin 6
board.SetPinMode(6,firmata.Input)
board.EnableDigitalInput(6,true)
go func() {
for buttonVal := range board.GetValues(){
_,val,_ := firmata.FirmataValue(buttonVal).GetDigitalValue()
if val[6]==true {
fmt.Println("LED ON")
board.DigitalWrite(9,true)
}else{
fmt.Println("LED OFF")
board.DigitalWrite(9,false)
}
time.Sleep(700 * time.Millisecond)
}
}()
// press ENTER to exit
fmt.Println("press Enter to exit..")
var input string
fmt.Scanln(&input)
fmt.Println("done")
}
$ go run main.go
4. Analog Sensor
In this chapter Im going to explain how to access sensor data on Arduino with sensor
device. In this section, I use go-firmata, https://github.com/kraman/go-firmata to develop
Go application to access sensor device on Arduino.
For building sensor hardware, we need 2 resistors, 50k ohm and 10k ohm. If your
thermistor is 10k ohm, you should use a resistor with 10k ohm.
The following is a circuit schema for Thermistor and LDR sensors.
One of sensor pin is connected to 5V Arduino pin. We use a divider approach. Thermistor
503 is attached to the Analog In of Arduino, A0. Otherwise, LDR is attached to A2.
You may attach LED as the indicator for sensor reading state. You can use a solution on
chapter 2, Blink app.
Here is a sample of hardware implementation.
You also can use Arduino shield which sensor devices are be connected, for instance, I use
Linker kit from http://store.linksprite.com/linker-kit/ with the following sensor items:
LDR, http://store.linksprite.com/ldr-ambient-light-module-of-linker-kit-for-pcduinoarduino/
Temperature sensor, http://store.linksprite.com/thermal-module-of-linker-kit-forpcduino-arduino/
}
// LDR from linker kit
// http://store.linksprite.com/ldr-ambient-light-module-of-linker-kit-for-pcdu
func CalculateLDR(sensorValue float64) float64 {
val := float64(1023-sensorValue)*10/sensorValue;
return val
}
// Temperature sensor from linker kit
func CalculateTempLinkerKit(rowADC float64) float64{
voltage := float64(rowADC) * 5.0
voltage /= 1024.0;
temperatureC := (voltage - 0.5) * 100
return temperatureC
}
This code works for LDR & Thermistor and Linker kit. You can remark one of them.
Save this code and run it.
You can see that LDR sensor value is converted to light information, for instance dark,
dim, light, bright, very bright.
Save this code.
5. RGB LED
This chapter explains how to control RGB LED connected to Arduino board using Go. We
explore how to access PWM (Pulse Width Modulation) Arduino.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
For Arduino Uno R3, you can see PWM pins as below.
We define a function, called setColor(). This function aims to write RGB values on PWM
pins.
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Now we control RGB values on RGB LED, for instance, Red, Green, Blue, Yellow,
Purple, Aqua.
void loop()
{
setColor(255, 0, 0); // red
Serial.println("red");
delay(1000);
setColor(0, 255, 0); // green
Serial.println("green");
delay(1000);
setColor(0, 0, 255); // blue
Serial.println("blue");
delay(1000);
setColor(255, 255, 0); // yellow
Serial.println("yellow");
delay(1000);
setColor(80, 0, 80); // purple
Serial.println("purple");
delay(1000);
setColor(0, 255, 255); // aqua
Serial.println("aqua");
delay(1000);
}
Compile and verify this code. If success, you can upload it to Arduino board.
If success, you can see RGB LED blinking with different colors. Here is a sample output
of RGB LED with Arduino Mega 2560.
5.3 Go Implementation
In this section, we create Go application to control RGB LED color. We use the same
scenario as previous section. For testing, I used go-firmata
package, https://github.com/kraman/go-firmata . Dont forget to load standard Firmata
library on your Arduino. Further information, you read it on section 3.1.
I have tested PWM on pin 9,10 and 11 didnt work. After printed all pin mapping, I have
the following pin mapping.
You print your pin mapping by modifying code client.go from go-firmata package as
follows.
func (c *FirmataClient) SetPinMode(pin byte, mode PinMode) (err error) {
if c.pinModes[pin][mode] == nil {
err = fmt.Errorf("Pin mode %v not supported by pin %v", mode, pin)
return
}
// print all pin mapping
and you also see RGB LED blinking with different colors.
Source Code
Contact