Raspberry Pi talks to Arduino using Dart


This post will show you how a Dart program running in a Raspberry Pi board can communicate to an Arduino board.

To show it we will send a text message from a Dart program running in a Raspberry Pi and we will visualize it in an LCD controlled by an Arduino board.


The main components are:

  • A Raspberry Pi Model B revision 1.0 running Raspbian 2014-09-09
  • An Arduino Duemilanove
  • A 16×4 LCD
  • breadboard, wires and cables

The Raspberry Pi board is connected to the Arduino board through an USB cable.


The LCD is wired to the Arduino board following the schema proposed in the Liquid Crystal Library example.



For the Arduino side I wrote a simple sketch using the Liquid Crystal library.

In the loop block the sketch reads a String from the USB and writes it to the LCD. The sketch replies to the Raspberry Pi board with an “Hello Pi!” message.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  //communication setup

  //LCD setup
  lcd.begin(16, 4);

void loop() {
  String read = Serial.readString();
  if (read.length()!=0) {
    Serial.print("Hello Pi!");

For the Raspberry Pi side I wrote a Dart program that sends a message to the USB port using the serial_portal package.

The program initializes the USB port and sets a listener for the incoming bytes in order to write them in the console. After the setup it opens the USB port and sends the “Hello Dart!” message.

import 'package:serial_port/serial_port.dart';
import 'dart:async';
import 'dart:io';

  var arduino = new SerialPort("/dev/ttyUSB0");
  arduino.onRead.listen((List<int> bytes){bytes.forEach(stdout.writeCharCode);});
  arduino.open().then((_) {
    new Timer(new Duration(seconds:3), (){
        arduino.writeString("Hello Dart!");
        print('sent "Hello Dart!"');

To run the program in the Raspbian environment I had to compile the Dart SDK.

In the screenshot you can see the Dart program running in the Raspbian terminal.



Here the source code: Dart-RaspberryPi-Arduino.zip

How to build the Dart SDK for the Raspberry Pi

For a DIY project I’m working on I need to run a Dart project on my Raspberry Pi board, precisely in his Raspbian OS. The Dart team has done a good job publishing a wiki page with all the information to build the SDK. Here I’m summarizing the necessary steps in a handy list, or, if you prefer, at the end of the post you can find a zip file with the SDK already built.


I suggest you run the entire process in a virtual machine in order to have a clean environment and the best compatibility with the provided scripts.

  • Download the Ubuntu server 12.04.5 64-bit ISO.
  • Install your virtualization software, I’ve used VirtualBox.
  • Create a new Virtual Machine. In VirtualBox using the wizard, select Linux OS and Ubuntu 64 bit. Here’s my VM configuration:
    • 4 GB ram
    • 23 GB HD, the used space after the entire process is about 11 GB
    • 2 processors (the core available in my hosting machine)
    • TCP ssh port forwarding (My host OS is a Linux with sshd active)
  • Install Ubuntu server through the downloaded ISO. During the configuration, in the package selection phase, select the OpenSSH Server, we will use scp to copy the produced SDK.


Login in Ubuntu and create a working directory. All the following commands are relative to the created working folder.


Download and run the script that installs all the necessary dependencies.

$ wget http://src.chromium.org/svn/trunk/src/build/install-build-deps.sh
$ chmod u+x install-build-deps.sh
$ ./install-build-deps.sh --no-chromeos-fonts --arm
$ sudo apt-get install libc6-dev-i386 g++-multilib


Download and setup the source code management tools provided for chromium.

$ svn co http://src.chromium.org/svn/trunk/tools/depot_tools
$ export PATH=$PATH:`pwd`//depot_tools


Download the Dart 1.7 source code (if you want the latest developed version use this url: http://dart.googlecode.com/svn/branches/bleeding_edge/deps/all.deps)

$ gclient config http://dart.googlecode.com/svn/branches/1.7/deps/all.deps
$ gclient sync


We are going to cross compile the code so we need the tool chain provided by Raspberry Pi.

$ git clone https://github.com/raspberrypi/tools.git

SDK compilation

Finally we are ready to compile. In the meanwhile you can go to take a coffee.

$ cd dart
$ ./tools/build.py -m release -a arm --toolchain=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf create_sdk


Copy the dart-sdk folder under the dart/out/ReleaseXARM folder to your Raspberry Pi machine (using scp or your folder explorer).

If you like to make the Dart commands available in the command line edit your .bashrc file in your Raspbian installation and add these lines (modify the dart-sdk path based on your installation path):

export DART_SDK=/home/pi/dart/dart-sdk
export PATH=$DART_SDK/bin:$PATH
export PATH="$PATH":"~/.pub-cache/bin"


You can check if the dart installation is working by simply running it. You should see this output:

pi@raspberrypi ~ $ dart
Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]

Executes the Dart script passed as <dart-script-file>.

Common options:
--checked or -c
 Insert runtime type checks and enable assertions (checked mode).
--help or -h
 Display this message (add -v or --verbose for information about
 all VM options).
--package-root=<path> or -p<path>
 Where to find packages, that is, "package:..." imports.
 Print the VM version.


As final step let’s run a simple Hello World project. We can create such simple project quickly using the new Stagehand tool. First we need to install it:

pi@raspberrypi ~/dart/projects $ pub global activate stagehand

We are now ready to create the project:
pi@raspberrypi ~/dart/projects $ mkdir HelloWorld
pi@raspberrypi ~/dart/projects $ cd HelloWorld/
pi@raspberrypi ~/dart/projects/HelloWorld $ stagehand consoleapp

The project is ready, let’s get hits dependencies and run it:
pi@raspberrypi ~/dart/projects/HelloWorld $ pub get
pi@raspberrypi ~/dart/projects/HelloWorld $ dart bin/main.dart
Hello world: 42!


If all these steps scares you, then you can download the zipped folder with the Dart SDK that I’ve prepared. You’re downloading it and using it at your own risk.