Month: February 2015

Controlling a USBTMC device with a Python Script

Recently I’ve needed to read the values of an i2c digital potentiometer for all 64 steps, using the Agilent e4980a precision LCR meter. The problem is, control of the LCR and logging of the data is typically implemented through LabVIEW, software I don’t know how to use, and I don’t have at my beck and call.

Fortunately, the LCR supports the USBTMC standard and there happens to be a Python library for USBTMC. The following Python script uses nested for loops to ask an Arduino Uno to increase the wiper position for a rheostat, and then triggers the LCR and records the impedance of the rheostat at 99 AC frequencies for all 64 steps.

Things needed:

USBTMC Python Library To control USBTMC devices

PyUSB For Python USB control

LiUSB 1.0 C USB Library

Your Python IDE of choice (I’m using PyDev)

PySerial To write and read from Arduino’s serial

Once you have that sorted, the Python script is straightforward.

from time import sleep

import usbtmc

import serial

lcr = usbtmc.Instrument(2391, 2313)

Arduino = serial.Serial(‘/dev/tty.usbmodem1421′, 38400, timeout=10)

for i in range(0, 4):

    print()

    for R1 in range(0, 64):

        # Instruct Arduino to change rheostat wiper

        Arduino.write(b‘1’)

        sleep(0.1)

        for f in range(0, 99):

            lcr.write(“:TRIG:IMM”)

            lcr.last_btag = 0

            sleep(0.01)

            data = lcr.ask(“FETC:IMP:FORM?”)

            sleep(0.01)

            print(str(R1) + “,” + str(f) + “,” + data)

The companion Arduino code is:

#include “Wire.h”
#include “AD5258.h” //Library for AD5258 functions (must be installed)

#define SSR 11
#define indicator_LED 12
AD5258 digipot; // rheostat r1
uint8_t wiper = 0;

void setup() {
Serial.begin(38400);
Wire.begin();

pinMode(indicator_LED, OUTPUT);

digipot.begin(1); // Specify i2c address for digipot
Serial.println();
}

void loop() {

if (Serial.available()) {
uint8_t ch = Serial.read();
uint8_t status;

if (ch == ‘0’) {
digitalWrite(indicator_LED, LOW);
Serial.println(“Switch off.”);
}

if (ch == ‘1’) {
if(wiper == 64) {
wiper = 0;
}
digitalWrite(indicator_LED, HIGH);
digipot.writeRDAC(wiper);
Serial.print(“Triggered. wiper is at “);
Serial.println(wiper);
wiper++;
}

} // end if serial available
} // end main loop

It works a charm and makes experimentation so much easier and faster.

Tinyduino BLE and Accelerometer demo

A demo sketch that carries Bosch’s BMA250 accelerometer data over BLE specifically for the TinyDuino BLE shield and accelerometer shield.

The sketch is available here: Sketch

An android app for it is available here: App

Note the app was designed for a more complex sketch, therefore all the functions in the app such as “Sample Rate” and “Frequency Sweep” will not be available. You can still export accelerometer data from the app to the phone’s SD Card however.

Have fun!