How to make an ultrasonic sercurity gate with Arduino Yun and Altair SmartWorks

0. Introduction

This tutorial helps you build a lego security gate.
To do this, we use an Arduino Yun, an ultrasonic sensor and a servo motor to lift and lower the gate. Every time the gate changes position we send that information to the Altair SmartWorks Engine.
In this project you will learn how to:

  • Connect an ultrasonic sensor to an Arduino Yun
  • Connect a servo motor to an Arduino Yun
  • Send streams to Altair SmartWorks using the CarriotsClientsYun library



  • 1 x An Arduino Yun
  • 1 x HiTec SRF05 Ultrasonic Sensor
  • 1 x HiTec HS422 Servo Motor
  • 1 x Tiny Breadboard
  • 1 x Ethernet Connection
  • 1 x Legos (with at least one gear)

Music to listen: Regina Spektor - Fidelity


1. Project Schema

This project is a quick way to create a securtiy barrier app and understand the basics of combining Arduino Yun, ultrasonic sensors, servo motors and Altair SmartWorks.

Here is the overall diagram of the project:

  • Schema

The Arduino Yun is programmed to register the presence or absence of objects in its proximity. According to this information, the lego gate will lift or lower. For every change of gate status, a stream is sent to Altair SmartWorks.

All the data streams sent by Arduino Yun are collected and stored in Altair SmartWorks. Altair SmartWorks is a huge database that collects all the information that your sensors send.

In addition to storing data, the true power of Altair SmartWorks is to let you build Apps very quickly with few lines of Groovy code.

With Altair SmartWorks you can build very complex Apps to support all the logic of your product on the cloud and integrate it with other devices and IT systems. But for now let’s keep it simple.


2. Arduino Yun. Connections description

You can see the connections between the Arduino Yun, the ultrasonic sensor and the servo motor in the diagram below. Note that pictured is the SRF04 ultrasonic sensor, we used the SRF05 ultrasonic sensor instead. They are completely compatible, the only difference is the order of the output prongs.

  • Diagram


3. Altair SmartWorks Project and Device Registration


If you're registered in Altair SmartWorks, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.


Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
You need this number to complete the example.


4. Data Transferred from Arduino Yun to Altair SmartWorks

In this tutorial we will use the CarriotsSendYun library. If you have not downloaded it you will need to in order to complete this tutorial. You can download the library and get more information about it in the tutorial How to use the Altair SmartWorks library for Arduino Yun to send streams.

The method used to send streams to Altair SmartWorks is the method "send". It has 4 parameters that you need to define in order to send a stream correctly (a two-dimensional array, the number of rows the array has, your Apikey, your device id_developer). The array consists of the key-value pairs that you will be sending to Altair SmartWorks.

Your payload, the stream to send to Altair SmartWorks, will be:

If you want more information about how to send streams to Altair SmartWorks using the Arduino Yun, see the Arduino Yun Streams Tutorial. You can also try the Arduino Yun Alert System Tutorial.


5. Code implemented on Arduino Yun

This is the code for the Arduino Yun. Note that some values should be replaced by your own values.


6. Checking Data is collected in Altair SmartWorks

Before setting up the lego component of the project, you can check to see if your device is sending streams when you put an object in front of and remove an object from the range of the ultrasonic sensor.

Run the program and test it by placing your hand in front of the sensor and removing it.

It's time to test our control panel and check if we have new streams.
Go to “Data management” → “Data streams” and you will see your data.

  • menu option
  • new data


7. Creating the lego gate

Making a security gate from legos can be done in many different ways, from very simple to very complex. It's really up to you to decide what type of design and complexity you want. Here, we will give you an idea of how you can make a simple gate that connects to your servo motor.

The basic components of making a gate are the gears that will change the gate's position and the gate itself, which must connect to the gear in a fixed position.

In the pictures below you can see how we have positioned the servo (with a gear fixed to it) under the gate gear. The gate itself consists of a long lego connected to the gear, which will turn as the servo motor turns, and the legos necessary to support the gate from the ground.

  • Gears
  • Gear position
  • Security Gate