Gobot with Arduino

Gobot with Arduino

Gobot is a framework for robots, drones, and the IoT devices written in Go language, it makes controlling robots and devices simple and fun. Gobot support numerous devices and platforms including Raspberry and Arduino.


Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects.

Getting started

Install Arduino IDE

You can load the code to Arduino using Arduino IDE. You can download the Arduino IDE for your PC from here. Once you have IDE installed you can start loading the sample sketches that are provided with IDE to Arduino.

To do this we need to connect the Arduino board to the PC through USB. Once the board is connected to the PC, make sure that you select the correct Arduino board in Tools | Board.

To check if everything is configured properly let's load a sample sketch that blinks the LED in Arduino.

  1. In the IDE go to File | Examples | Basics | Blink
  2. Click on Verify
  3. Click on Upload

If the setup is working fine, the LED on the Arduino should be blinking.

If you observe the Blink sketch in IDE you see that the code written in C. We are basically editing the C programs in Arduino in IDE and uploading it to Arduino. Now, we will write the code in Golang which has more capabilities than C, and run in on Arduino.

We cannot run Go programs directly on Arduino. This is where we use a framework called Gobot.

Install Gobot

In order to run programs written in Golang on Arduino, we will install Gobot on our PC.
Install Gobot on your PC (This assumes you already have Go installed)

> go get -d -u gobot.io/x/gobot/...

Load Firmata to Arduino through IDE

Gobot uses Firmata protocol to communicate with Arduino. Latest Arduino IDE comes with Firmata library and that needs to be loaded into Arduino.

To do this, with Arduino connected to PC follow the below steps:

  1. In Arduino IDE, open File | Examples | Firmata | StandardFirmata
  2. Click on Verify, then on Upload.
  3. After some time IDE will display "Done Uploading" message.

Now, we are ready to use Gobot with Arduino.

Hello World Gobot program

Copy the below code from Gobot Getting started to helloworld.go and save it.

package main

import (
    "time"

    "gobot.io/x/gobot"
    "gobot.io/x/gobot/drivers/gpio"
    "gobot.io/x/gobot/platforms/firmata"
)

func main() {
    firmataAdaptor := firmata.NewAdaptor("COM3")
    led := gpio.NewLedDriver(firmataAdaptor, "13")
    work := func() {
        gobot.Every(5*time.Second, func() {
            led.Toggle()
        })
    }
    robot := gobot.NewRobot("bot",
        []gobot.Connection{firmataAdaptor},
        []gobot.Device{led},
        work,
    )
    robot.Start()
}

Run go

Now that we have our Arduino and code ready, it's time to run our application. To run our application, in the command line,

> go run helloworld.go

If our setup worked you should see the LED blinking on Arduino. You can play around by changing the timeout value in the code.

Common Issues

  • LED is not blinking after running helloworld.go
    Check the port in Arduino IDE in Tools | Serial Port and make sure that the port is updated in the below line of the above code.
   firmataAdaptor := firmata.NewAdaptor("COM3")
  • Unable to compile Go program
    This could be because of the wrong import of Gobot libraries. Make sure you have the below libraries in your GOPATH.
        "gobot.io/x/gobot"
    "gobot.io/x/gobot/drivers/gpio"
    "gobot.io/x/gobot/platforms/firmata"