Battery Tester

Measure the voltage of a AA battery using your VINT Hub Phidget!

Parts

Aside from the items below, you will also need the following:

  • AA Battery
  • Wire Stripper
  • Alligator Clips (optonal)

USB cable

Phidget cable

Setup

Step 1

Cut a spare sensor cable in half.

Note: if you do not have alligator clips, make sure to leave enough wire to connect to both sides of a AA battery.

Step 2

Remove about 1 inch (2.5cm) of insulation from the black wire and the white wire. Remove the red wire.Cut a spare sensor cable in half.

Step 3

Connect the cable to Port 0 on your VINT Hub Phidget.

Step 4

Connect one end of your alligator clips to the bare wires and the other end to the battery.

Note: the white wire should be connected to the positive terminal on the battery.

Write code (Java)

Copy the code below into a new Java project. If you need a reminder of how to do this, revisit the Getting Started Course.

Not your programming language? Set your preferences so we can display relevant code examples

  
//Add Phidgets Library
import com.phidget22.*;

public class BatteryTester {

    public static void main(String[] args) throws Exception {
        //Create
        VoltageInput batteryVoltage = new VoltageInput();

        //Address
        batteryVoltage.setHubPort(0);
        batteryVoltage.setIsHubPortDevice(true);
        
        //Open
        batteryVoltage.open(1000);
        
        //Use Your Phidgets
        while(true){
            System.out.println("Battery Voltage: "  + batteryVoltage.getVoltage() + "V");
            Thread.sleep(250);            
        }        
    }
}  
  
  
package batterytester;

//Add Phidgets Library
import com.phidget22.*;

public class BatteryTester {

    public static void main(String[] args) throws Exception {
        //Create
        VoltageInput batteryVoltage = new VoltageInput();

        //Address
        batteryVoltage.setHubPort(0);
        batteryVoltage.setIsHubPortDevice(true);
        
        //Open
        batteryVoltage.open(1000);
        
        //Use Your Phidgets
        while(true){
            System.out.println("Battery Voltage: "  + batteryVoltage.getVoltage() + "V");
            Thread.sleep(250);            
        }        
    }
}  
  
  
Code is not available for this project.
  

Write code (Python)

Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course.

Not your programming language? Set your preferences so we can display relevant code examples

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageInput import *
#Required for sleep statement
import time
 
#Create
battery_voltage = VoltageInput()

#Address
battery_voltage.setHubPort(0)
battery_voltage.setIsHubPortDevice(True)

#Open
battery_voltage.openWaitForAttachment(1000)

#Use your Phidgets
while(True):
    print("Battery Voltage: " + str(battery_voltage.getVoltage()) + "V")
    time.sleep(0.25)  
  

Write code (C#)

Copy the code below into a new C# project. If you need a reminder of how to do this, revisit the Getting Started Course.

Not your programming language? Set your preferences so we can display relevant code examples

  
using System;
using System.Collections.Generic;
using Phidget22;

namespace BatteryTester
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create
            VoltageInput batteryVoltage = new VoltageInput();

            //Address
            batteryVoltage.HubPort = 0;
            batteryVoltage.IsHubPortDevice = true;

            //Open
            batteryVoltage.Open(1000);

            //Use your Phidgets
            while (true)
            {
                System.Console.WriteLine("Battery Voltage: " + batteryVoltage.Voltage + "V");
                System.Threading.Thread.Sleep(250);
            }            
        }
    }
}
  

Write code (Swift)

Copy the code below into a new Swift project. If you need a reminder of how to do this, revisit the Getting Started Course.

Not your programming language? Set your preferences so we can display relevant code examples

  
Code not available.
  

Run Your Program

Your program will output the battery voltage.

Practice

  1. Try using the LEDs from your Getting Started Kit to create a battery testing device. Turn on the green LED if the battery is new, and the red LED if the battery needs replacing!

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done