Introduction: Building a Simple Pendulum and Measuring Motion With Arduino and Python
In a previous Instructable, I shared my excitement about the potential of the Arduino in the classroom and explored the use of Arduino as a data-logger in a simple toy-system where we measured water pollution. In this Instructable, we'll build a classic and important physical system, the simple pendulum. We'll use an Arduino and a potentiometer to measure the amplitude of the pendulum's motion and Python to read and visualize our data. The project here is designed in such a way that the pendulum arm can be easily swapped out for arms of different lengths or even compound pendulum arms. So, once built, the basic system allows for a full range of experiments - from measuring the period of a simple pendulum to studying the nonlinear dynamics of a double pendulum.
Before we begin, let's take a quick look at the completed system. In the pictures above you'll see a front and side view of the completed system. On the front, you'll see the pendulum arm, rotating on a bearing, suspended on a rigid rod. Attached to the pendulum arm is a simple gear that rotates around the same axis as the pendulum. This gear drives an identical gear attached to a potentiometer mounted adjacent to the pendulum arm. Moving to the rear, you'll see the potentiometer wired to an Arduino mounted to the back of the L-shaped wood supports that hold the entire setup. As the pendulum arm moves back and forth, the potentiometer is turned through an identical, but opposite angle as the pendulum. Readings from the potentiometer are captured with the Arduino and fed to a Python script that stores and plots the motion of the pendulum.
Step 1: Parts List
For the mechanical part of the project you'll need the following:
- Skateboard bearing (You really only need one, but with more, can make multiple interchangeable arms.)
- Steel rod, 5/16" in diameter, 6" length (Can be found in the hardware section of Lowe's or Home Depot, but you'll need to cut to length. Alternatively, you can order a 6" piece from McMaster-Carr.)
- Flat aluminum bar, 1/8" X 1 1/4" X 3' (Can be found in the hardware section of Lowe's or Home Depot. Alternatively, you can construct the arms from wood or acrylic.)
- Scrap wood
For the data acquisition part of the project you'll need the following:
- Arduino (I used an Arduino Uno Rev 3, but any Arduino should work.)
- Potentiometer (I used the Grove Rotary Angle Sensor because I happened to have one on hand. It's by no means necessary to use a Grove Sensor here. Any potentiometer such as this one will do the trick. You may need to modify the mounting of the potentiometer to the board if you use a different potentiometer.)
- Grove Base Shield (Only necessary if you use the Grove Sensor. If not, I'll show the wiring for a standard potentiometer in a later step.)
To construct the gears you'll need access to a laser cutter or the ability to otherwise construct or salvage two identical gears from something else. The svg file for the gears appears below.
Finally, you'll need access to the following tools:
- 13/16" metal drill bit (or standard bit if constructing arms from wood or acrylic)
- Dremel with metal grinding bit
Step 2: Build Your Pendulum Arm
For the pendulum arm, I used the same basic construction technique as in this project. Note that while here, I've specified a flat aluminum bar as the material from which to make the pendulum arm, you can use other materials such as wood or polycarbonate. The aluminum bar specified has the correct width to be used with the bearings, so make sure any alternative you choose has the same or larger width. For this step, you'll need your gears, so either laser cut using the SVG file provided, or have the gears you intend to use at hand.
To build the pendulum arm, you're going to drill a hold in the bar using the 15/16" metal drill bit. The center of the hole should be about 15 mm from the top of the bar and centered across the width. Once you've drilled the hole, check the fit of your bearing. At this point, the bearing should be too big to fit in the hole. Using the Dremel and grinder bit, carefully increase the radius of your hole until the bearing can be gently tapped into place. Once the bearing is firmly in place and flush with one side of the pendulum arm, flip to the flush side and glue your gear to the arm, centered on the bearing. For the glue, I used E6000 and allowed it to dry for about 12 hours. Whatever glue you choose, remember that the gear must be firmly attached to the pendulum arm.
Step 3: Build Your Support and Attach Your Sensor
The L-shaped support is simply composed of two scrap pieces of 3/4" thick plywood, each piece being about 8 1/2" X 4 1/2". You want to make sure the L-shape is rigid, so whatever joinery method you choose, aim for strength. I used pocket screws and wood glue which was more than sufficient.
Once you've constructed your support, affix your potentiometer to the front face, so that it's center is about 1 1/2" from the top and 1" from the left side. The Grove Sensor can be affixed using wood screws. If you are not using a Grove Sensor, you'll need an alternative method of attaching the potentiometer to the support. Once the potentiometer is attached, press-fit the appropriate gear to the shaft of the potentiometer.
Important note - The Grove Sensor and the potentiometer linked earlier have a swing of 300 degrees. This means that the potentiometer will rotate through 300 degrees and that there is a 60 degree portion of the circle defined by the potentiometer that is inaccessible. You want to make sure that this inaccessible portion is pointing up when you affix your potentiometer to the support. That is, you want the potentiometer positioned so that it can capture as wide of a range of motion of the pendulum as possible.
Step 4: Attach Pivot for Pendulum Arm
In this step, you will drill a hole in the L-shaped support that will allow you to snugly fit the 6" steel rod. The positioning of the hole depends upon the exact positioning of your geared potentiometer. The best strategy to mark the location of the hole is to insert the steel rod through the bearing on your pendulum arm, position the arm so that the gear from the arm and the gear from the potentiometer mesh smoothly, and mark the location of the steel rod. The crucial part of this step is ensuring that the gears mesh smoothly and that the pendulum arm swings freely.
Tip - if you're pendulum gear and potentiometer gear are not perfectly aligned, the gears may "unmesh" over time. Adding a properly sized rubber gromet to both sides of the pendulum on the steel rod will help keep the gears aligned, but will introduce additional friction into your system. Generally, I've found the gromet is not needed if you are careful about ensuring that both gears lie in the same plane.
Step 5: Connect and Program Your Arduino
If you are using the Grove Sensor, you simply want to connect the 3-wire cable from the sensor to the A0 port of the Grove Shield. If you are not using the Grove system, wire your Arduino to your potentiometer following the diagram above. Note that one wire goes to 5V, one to GND, and one to A0. It is also convenient to secure the Arduino board to the L-shaped support.
Since we'll be using Python to read and plot data from the system, the code on the Arduino side is relatively simple. The Arduino code needs to get a reading from the potentiometer, and send it as data via the serial port in a way that we can interpret with Python. The Arduino code you'll need is pasted below.
Note that the baud-rate for the Arduino must be set to the same baud-rate we'll use with Python. Once you have uploaded your code to the Arduino, test your system by opening the Arduino serial monitor and moving the pendulum arm. You should see a range of values from 0 to 300 in the serial monitor as you move the pendulum arm.
Step 6: Write Your Python Code
Below, you'll find the Python code that reads the data from the Arduino, stores the data in a vector, and plots once the measurements are complete. Note that this is Python 3, modifications may be needed for Python 2. Also note that you'll need to install the serial package via pip install PySerial if you have not done so previously.
The code below opens a serial connection to the Arduino, takes a set number of samples, stores the samples and the time of each sample, plots the data, and then closes the connection to the Arduino. When you conduct an experimental run, you will set two values in the code. First, you'll set the value of "samples," telling Python how many data points you wish to gather. In the code below, this value is set to 300. Second, you will tell Python how your pendulum is oriented with respect to readings from the potentiometer. To do this, allow your pendulum to hand straight down and motionless. Open your Arduino serial monitor and note the value returned. Enter this value in the code as the constant called "calibrate." Python will then use this as the zero position of your pendulum arm. Note that here we are working in radians and we are converting the degree measurements from the Arduino to radians within the Python code.
Step 7: Test and Experiment!
Above you'll find two plots constructed using this system. The first shows data for the simple pendulum single pendulum arm we constructed. The second, shows the motion of a compound pendulum shown in the picture above. The simplest way to test the accuracy of your system is to compute the theoretical period of your pendulum and compare it with the measured value. For the measured value, you simply find the x-value, or time values, at neighboring peaks in your plot, and take the difference. This is your measured period. For the type of pendulum arm we constructed, the theoretical value of the period is given by (2*pi*L)/(3*g*R)^0.5 where L is the length of the bar, R is the distance from the pivot point to the center of mass of the bar, and g is the gravitational acceleration of the Earth. For my bar, I predicted a period of 1.6 seconds and consistently measured a period of 1.59 seconds. Pretty good!
Since you can easily interchange pendulum arms with this system, you can now conduct a wide-range of experiments and expect high-quality data. You may want to try pendulum arms of different lengths or try to understand the motion of compound pendulums like the one pictured above. Whatever you decide to do, I hope you'll find this simple Arduino-Python sensor system a useful tool for your explorations!
We have a be nice policy.
Please be positive and constructive.