Vernier Software and Technology
Vernier Software & Technology
Arduino and Vernier Sensors

Levitating Ping Pong Ball

If you are looking for an advanced Arduino challenge, try this levitating ping pong ball project. It involves placing a ping pong ball in a tube with a fan at the bottom of it and placing a Motion Detector at the top of the tube to measure the distance to the ping pong ball. Collected data is evaluated in a PID library and the output is used to control the fan speed. You need to set the constants for proportionality, integral and differential (P, I, and D), or simply use the values that we have used in optimizing the PID feedback loop.

You are able to set the height of the ball by changing the variable “Setpoint” in the program. We have previously created a version of the program that advances through five different heights, pausing for 10 seconds at each one.

The ping pong ball finds equilibrium at the set point. VPython is modeling the motion of the ball.
The ping pong ball finds equilibrium at the set point. VPython is shown modeling the motion of the ball.

There are several opportunities for advanced exploration with this project. You can build your own PID control algorithm rather than importing the PID library. You can also research how to use a PID optimization library that will automatically choose parameter values for you. We have created a VPython program that modeled the ball in the tube based on data from the Motion Detector. For more information on this last element, please refer to Using Arduinoâ„¢ and VPython to Explore PID Control.

VernierMotionDetectorPID (v 2017.08)
Takes data from a Vernier Motion Detector connected to Digital 1 connector. 
Uses PID to control fan to elevate ping pong ball to target height. The ball is in
plastic tube 0.6 m tall with a fan at the bottom and a motion detector at the top. 
This version uses a Digital Control Unit (DCU) connected to the Digital 2 port to 
control the fan.

Motion detector is read using the Vernier Library that calculates the distance based
the echo of an ultrasonic sound wave. 

The PID control is modified from 
and takes the calculated input from the motion detector ("distance") and controls analog PWM
output 6 through the DCU (line 1).You will need to download the PID library from

As written, the reading will feed to the PID control continuously and data fed to
Serial Print.

There are several ways you can modify this. We have previously linked the output 
of this program to VPython to create a virtual display of the ping pong ball. 
Search the internet for tips on combining Arduino and VPython. You can also vary 
set point so that it seeks a variety of heights - definitely more eye catching.
See for more information.
#include <PID_v1.h>
#include "VernierLib.h" 
VernierLib Vernier;
double setPoint, distance, output; //define variables we'll be connecting to
PID myPID(&distance, &output, &setPoint, 3,6.0,6.0, REVERSE); 
/*Specify the links and initial tuning parameters. These work for a system
with a 0.6 m tube and the particular fan we are using. Depends on power supply 
to the fan as well as cowling around fan.*/

void setup()

  setPoint = 40; // setpoint (cm from top)
  myPID.SetMode(AUTOMATIC); //turn the PID on

  Serial.println("Vernier Format 2");
  Serial.println("Motion Detector Readings taken using Ardunio");
  Serial.println("Data Set");

  Serial.print("Set Point");
  Serial.print("\t"); // tab character
  Serial.print ("Distance"); //long name
  Serial.println ("Output from PID"); 

  Serial.print("SP (cm)");
  Serial.print ("d (cm)"); //short name
  Serial.println("0 - 255");
void loop() 
  distance = Vernier.readMotionDetector();
  delay (10);



Vernier Engineering Contest

Vernier recognizes instructors for creative uses of Vernier sensors to introduce engineering concepts and/or practices. Vernier Engineering Award »

Go to top