Harry McNamara

how to make (almost) anything

week 10: interface applications

As most of the hello.world examples this week demonstrated serial input from ATtiny boards to python applications, I thought I would play around with the opposite direction: using a python Tkinter program to set a byte value to an ATtiny board, and setting a PWM value using that byte (e.g. for audio output).

After installing the Tkinter package, I adapted the hello.load code I worked with last week to make this hello.slider minimal Tkinter GUI to control a sliding scale. The argument:

Slider_1 = Scale(root, to=255, variable = val, command = get_val)

Defines a slider ('scale' according to Tkinter) from 0 to 255, whose value is 'val', and whose value can be returned using get_val()

example Tkinter scale/slider

In hello.slider.py, the get_val(self) function is defined to get the value on the slider, then send it using the pyserial ser.write() function. After playing around with print statements to print sent bytes to the command window, I realized that I needed the idle window to recursively call itself after a delay in order to keep outputting values. The specific relevant line is:


I adapted another one of my boards (outline) to receive the input using this code). Checking the output on the oscilloscope, I noticed some weirdness: first, when I used the 102 us delay for 9600 baud communication (as used in Neil's C code) I could cycle PWM duty cycles by adjusting the slider, but it never went to zero (indeed it looked like it always read chars with 0xF(something determined by the scale). Moreover, when I did send the framing bytes, the ATTiny didn't see them at all and just kept waiting for the framing.

In the end I found the best performance by *not* sending framing bits, and setting the ATtiny bit delay to 51. On this setting, the PWM cycles reliably from 0% to 100% when the Tkinter slider was cycled between 0 and 63. (at 64 it went back down to zero, and up to 100% at 127, etc...). Attaching a small peripheral speaker allowed you to make minal audio (crackles) by moving the slider; in principle this could be used to set e.g. the frequency of a sine wave output, or send more complicated serial audio through the FTDI (if the python read e.g. a .wav file rather than sending a slider value).

(standing by itself) a careful balancing act to set the oscilloscope probe while adjusting the python slider valueexample Tkinter scale/slider
another view
visualization of PWM period
pwm demo
crackle crackle

I also had some ambitions of using the accerlometer hello.world board to give input and act as a controller for a pong-style game (using pygame) but alas, the scarcity of time as finals approached precluded this. Perhaps in weeks future.