Arduino

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!

Extracting Bio-impedance Parameters from Measured Data

First post of the new year! I figured, I’d start with something interesting.

Over winter break, I went home to the lovely warmth of West Africa, and had an epiphany of sorts as to how to extract the resistive values for intracellular water (ICW), extracellular water (ECW) and the capacitive value for the cell membrane after performing a frequency sweep on biological tissue (or model circuit) of interest.

(more…)

Creating custom timer interrupts for arduino

Arduino boards are awesome. Even more awesome is the fact that there’s so much documentation out there on using them.

A very popular timer interrupt library for Arduino is MsTimer2, which has a resolution of 1 millisecond. If your project requires more resolution however, you’re a bit out of luck. Fortunately, it’s rather easy to increase the resolution of the timer library with a few modifications.

(more…)

Hypothesis confirmed: Software serial libraries are bad for complex sketches

Earlier today I tested the Z_BLE_3 sketch on an Arduino Mega using its hardware serial and the results were fantastic.

It just goes to show, you may have a pretty good software serial library, but it’s nowhere near as good as the real hardware serial.

Downsides of software serial libraries

The default Arduino SoftwareSerial library proved to be non-tolerant with simple libraries when used for BLE communication, even more so with fairly complex libraries on top of that.

We switched to Paul Stoffregen’s AltSoftSerial library and things have been great… till now. From the documentation:

Interrupt Latency Requirements

AltSoftSerial can withstand approximately 1 bit time of interrupt latency from other libraries or functions. Most libraries, HardwareSerial, and the millis() timekeeping interrupt are designed to minimize CPU time spend executing interrupts or accessing data with interrupts disabled.
However, some libraries may disable interrupts for longer than 1 bit time. If they do, AltSoftSerial will not work properly. If you discover erroneous results from AltSoftSerial, please try testing without other libraries.

SoftwareSerial would break with relatively low complexity I2C communication using Arduino’s Wire library, it proved impossible to use with the AD5933 library for similar reasons.

AltSoftSerial works very well in this regard, but as we begin to write multiple sampling rates and frequency sweeps that request timer interrupts and reset commands to the AD5933 more frequently, the time window for interrupts to be handled shrink considerably.

With the sketch BLE_Debug_Timer that just carries a psuedo sine signal, setting a sampling rate of 90 Hz is no issue. Z_BLE_3 however that has to measure and send impedance values, breaks at sampling rates over 80 Hz and completely garbles serial communication with AltSoftSerial library, you only need to look at Arduino’s serial monitor to see the gibberish it spouts and functions it calls that it really shouldn’t.

We therefore probably need to move to the Arduino Mega for development and use the multiple hardware serial port it provides for BLE communication.

All this of course is a working hypothesis, I will be testing my claims tomorrow in lab, but I’m fairly certain they’re right from past experience with SoftwareSerial and AltSoftSerial.

On we go again.