120 Beacon St, Somerville, MA 02143
Automatic Object Detection with your NeuroMaker Hand

Allow your NeuroMaker hand to detect an object near it and automatically perform gestures. For example, you can place the sensor on the palm so it automatically shakes your hand when you approach with your hand. Learn how to use breadboards, sensors, and basic circuits. Code C++ in Arduino IDE to power the sensors, get input and output, visualize serial input and create your own gestures and applications.

Estimated Time

2 hours

Number of People

1- 2 students per NeuroMaker Hand Kit

Necessary Supplies

  • One fully build NeuroMaker Hand
  • (Included in the kit) UNO programming board
  • USB cable (Included in the kit)
  • Jumper cables and bread board (Included in the kit)
  • HC-SR04 ultrasonic distance sensor (Not included)

Necessary Program Files

Pre Check Items

  • Completion of “Build your first C++ program” project
  • The NeuroMaker Hand is fully built
  • Both 9V and AA batteries are installed
  • A Mac/Windows computer with network access and USB port
  • Arduino IDE is installed


C++ is one of the most popular programming languages in use around the world today. Much of the code that runs your internet browser and popular online games like World of Warcraft use C++ to run their systems! C++ requires less resources to operate than other higher level languages like Python. In the world of Biomedical sciences, C++ is used from everything to power MRI machines to systems that handle patient information. At BrainRobotics, much of the code used to control the movement of the device itself is actually written in C++!

Project Instructions

Connect the ultrasonic sensor to the blue power chip in the NeuroMaker Hand using breadboard and jumper wires.

Below is a list of connections:

Pins on the ultrasonic sensor Sockets on the blue power chip
Vcc 5V
Trig D7
Echo D8


Below is the instruction for the code.

You have already learned in the previous C++ project that we need libraries at the start to import some important pre-built code:

Complete This C++ Code!

#include <Servo.h>

// Next, create 5 Servo objects called "servos" and save them in an array.

// Complete your code here:

/* Now lets define some pins. On the Ultrasonic sensor, you can find "Trig" and "Echo".
Since we connected Trig to D7, and Echo to D8, we need to tell the microprocessor.
The max and min distance defines the range which you want the fingers to move. If the input
distance is more than max, fingers do not move. If the distance is closer than the min, all
fingers move 100%. For any number in between, we need to calculate the angle so fingers move linearly.
const int trigPin = 7;
const int echoPin = 8;
float duration, distance, angle;
float maxDistance = 15;
const float minDistance = 5;

void setup() {

// Define the input/output mode for each pin.
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Make sure the trig pin sets to LOW at the beginning.
digitalWrite(trigPin, LOW);

// initialize serial communication:

// In the previous project you learned how to connect each Servo object
// to a specific pin.

// Complete the rest of the code:

// Make sure all fingers are in open position at the beginning.

void loop() {

// The trigPin is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

/* When the sound waves generated from the sensor module hit a barrier, they bounce back and
are captured by another receiver which turns the Echo pin HIGH for the time duration of the waves traveling.
To get the time duration of wave traveling, we can use a built in Arduino function called pulseIn(). It takes 2
arguments, the pin# and a state we want it to return the time(HIGH or LOW). In our case, when it detects that
the echoPin is HIGH, it will start timing and then stops timing when echoPin switches to LOW. Then we simply
store the time in the duration variable.

duration = pulseIn(echoPin, HIGH);

/* convert the time into a distance. Since speed = distance/time, and the pulseIn returns time in Microseconds,
we can search from the internet that the speed of sound is .0343 c/μS. Just multiply the duration we got from
the previous step by .0343 and then divide it by 2(Since the sound waves travel to the object and then travel back).

distance = (duration*.0343)/2;

// We can see the real time input from the sensor using Serial monitor. Go to
// "Tools" and select "Serial monitor".
Serial.print("Distance: ");
// This is a function we define to ge the calculated result of the angles servo motors needs to move.
angle = getAngle(distance);

Serial.print(" Angle: ");

// This is a function we define to move all fingers based on the angle input.

/* This is the body of the function. It returns the absolute value of the angle displacement of servo motors.
If the input variable "distance" is larger than the maxDistance, return 0. If the input is closer than the
minDistance, return 180. For any input in between, linearly map the input to the angle.
float getAngle (float distance){

// Complete this function


// This function moves all fingers to their original position. Refer to the previous project
// for how to achieve it.
void resetFinger(){

// Complete this function


// This function moves all fingers to the target position, based on the angle inpuit. The input is
// the absolute value of the displacement angle.
void moveFinger(float angle){

// Complete this function



Serial Data Display

Your serial monitor will display sensor data as the image to the right.

Upload Your Code

After the code is done, it’s time to start uploading. Use the USB to connect the NeuroMaker hand with your computer. In the Arduino IDE, go to “Tools” and choose the port connected to your hand as shown below. The port # might be different on your device.

Choose board as Arduino Uno as shown.

Run Your Code

Click the upload button shown in yellow to upload your program to the NeuroMaker Hand.



How did your code work? Could you create the right motor angles to display different hand signals? What other logic could you add to make your program work more efficiently? For hints, view Sensor_Answer.ino