arduino programming via soundcard

with lots of study of the AudioBoot_V2_0 java code by chris at roboterclub-freiburg.de, i managed to write supercollider code for uploading sketches to an arduino via the soundcard. no ftdi chip needed!
it's a very cheap solution for programming barebone arduinos (well, atmega168 microcontrollers really). you only need a few resistors, a capacitor and a mega168. the only difficult part is to 'initialize' this microcontroller by burning the special bootloader on to it. this requires an avr programmer of some sort (STK500, USBtinyISP etc).

the preparation steps are as follows...
1. burn the bootloader on to a mega168
2. build the barebone circuit
3. prepare the arduino ide
4. install the RedArduino class

after that one can compile hex files in the arduino ide and upload them using supercollider and a standard audio cable.

1. burn the bootloader on to a mega168

the trick behind all this is the special 'sound enabled' bootloader that i found here... http://www.hobby-roboter.de/forum/viewtopic.php?f=4&t=127. i downloaded the AudioBoot_V2_0.zip file and used my stk500 avr programmer together with the great avr crosspack. the terminal command i used for burning the bootloader was the following...

avrdude -v -p m168 -b 115200 -P /dev/tty.PL2303-000013FA -c stk500v2 -U flash:w:/Users/Stirling/arbeten/sc/scAudioino/AudioBoot_V2_0/Atmega_Source/chAudioBoot_ATMEGA168_IN_PD1_LED_PB5.hex -U lfuse:w:0xE2:m -U hfuse:w:0xDF:m -U efuse:w:0xFA:m

2. build the barebone circuit

then i built a minimal and barebone arduino circuit after the schematics found here... http://www.hobby-roboter.de/forum/viewtopic.php?f=4&t=128&p=531. again credit to chris.

this is the schematics i drew...

and here the resulting circuit...


i run it off 4,5v (3 batteries) but it could also be powered from the usb port.

3. prepare the arduino ide

to compile hex files for this barebone arduino, i needed to set up a custom board in the arduino ide. one way to do this is to create a new text file called boards.txt and put it inside a new folder in the arduino/hardware folder. on mac osx that could be something like ~/Documents/Arduino/hardware/BareBones/boards.txt. the boards.txt should contain the following...

minimal168.name=ATmega168 bare bone (internal 8 MHz clock)
minimal168.upload.speed=115200
minimal168.bootloader.low_fuses=0xE2
minimal168.bootloader.high_fuses=0xDD
minimal168.bootloader.extended_fuses=0×00
minimal168.upload.maximum_size=16384
minimal168.build.mcu=atmega168
minimal168.build.f_cpu=8000000L
minimal168.build.core=arduino:arduino
minimal168.build.variant=arduino:standard

then restart the arduino ide and under boards there should be a new option with mega168 and 8mhz internal clock. i make sure this board is selected every time before compiling hex files for sound uploading.
another thing that needs to be done is to enable 'verbose compile' in the arduino ide preferences. that will print out the file path of the hex file each time you compile a sketch.

4. install the RedArduino class

i wrote a couple of classes for supercollider to help with the encoding and signal generation of hex files. they're found in my redSys quark (under redTools) and are most easily installed from within supercollider itself with these commands...

Quarks.checkoutAll;             //update
Quarks.install("redSys");       //install. recompile after this

there are two helper classes and one main class. the RedIntelHex class parses hex files and RedDifferentialManchesterCodeNegative helps to encode the signal as differential manchester code. the main class RedArduino figures out the paging of data and generates a bit stream that is played back using demand rate ugens.

uploading

i upload sketches by compiling them in arduino ide (click verify - not upload) and copy&paste the file path of the resulting hex file into supercollider and the RedArduino's read method. i connect the left sound output channel to the barebone arduino, put my mac volume to ~80%. last i press the reset button on the circuit and quickly (within seconds) call the upload method in sc. the led should blink slowly directly after a reset, and fast when receiving data.

here's a video demonstrating how to do it. i'm just uploading the simple Blink example. near the end you will also hear how that sounds.

audioino from redFrik on Vimeo.

there's also some sc code i wrote here that will do the same thing but without the need of the redSys quark classes.

trash or treasure

some weeks ago i found a canon pixma MP510 on the street. a "Photo All-in-One with economical single inks that prints, copies or scans in colour". someone had thrown it away regarding it as a piece of junk. i saw a pile of gold.

taking it apart i got...
* four really nice motors: two steppers and two dc motors
* a 33x25 cm piece of glass
* boxed and reusable 220v powersupply
* a small lcd colour display
* a scanner head
* some leds
* five infrared sensors (paper sensors)
* lots of screws and springs
* wire and cog belts
* some switch buttons
* two usb jacks
* various other components



alike http://makezine.com/projects/dont-waste-ewaste-unmaking-a-canon-printerscannerfax-into-parts/

usb soundcards mod

to save a bit of power (and annoyance), i de-soldered the leds on two usb soundcards. i use these soundcards for battery driven projects (beaglebone black) and every milliamp i can save counts.
the logilink soundcard had two easily removable leds. the red one indicated that the soundcard was connected and had power, and the green one started to blink when the card was in use (driver activated). both functions i can easily live without.
the blue '3d-sound' card had a very tiny surfacemount led that i removed using two soldering irons.

here some before and after photos...




btw, i'd stay away from the logilink. it has a problem with audible noise coming from the pwm signal of the green blinking led. if you connect a mic like i'm doing, a beep beep beep kind of sound leaks into the mic. and removing the led doesn't help. maybe there's something in the software driver to control it, but i doubt it.

cheap 4-channel videoplayer

for the dance piece ich(a) by zufit simon i constructed a system with four raspberry pi mini-computers and buttons to trigger playback of four video streams. as the videos didn't need to run in exact frame-by-frame sync, this was a very cheap way to get four channel high-quality video playback. total cost was about (rpi 28*4)+(sdcard 6*4)+(5v power 1*7) ≈ 141 euro. i chose the model A of the raspberry pi to keep the cost and power consumption down. the four computers share a 5v power supply of 2 amps and are powered over the gpio pins. video cables run 50 meters down to the stage and in to separate flat screen monitors. the monitors are built in to boxes that can be piled up or rolled around independently.

the videos are stored on the 4GB sd cards that also holds the linux operating system. i converted the videos from dvd to mp4 using ffmpeg with the following settings...

ffmpeg -i concat:"/Volumes/MONITOR01_may2012_DVD/VIDEO_TS/VTS_01_1.VOB|/Volumes/MONITOR01_may2012_DVD/VIDEO_TS/VTS_01_2.VOB" -an -vcodec libx264 -profile:v high -preset fast -crf 18 -b-pyramid none -f mp4 MONITOR01_may2012.mp4

that'll take two chapters and convert to a single mp4 and skip the sound track (-an flag).

the python program running on each computer is here below. it plays a video to the end and waits for a button trigger. if a button is pressed before the video is finished, it'll stop and jump to the next video - all in a cyclic fashion.

#f0videoplayer.py
#for a raspberry pi running raspbian
#this script will cycle through videos in sequence when a GPIO pin is grounded

#pinSwi (pulled up internally) - gnd this pin to switch to the next video
#pinOff (pulled up internally) - gnd this to shut down the system

#--settings
videos= ['/home/pi/ICHA1.mp4', '/home/pi/MONITOR01_may2012.mp4', '/home/pi/BLACK.mp4', '/home/pi/FLESH.mp4', '/home/pi/TESTBILDER.mp4']
delays= [0, 0, 0, 0, 0] #extra start delay time in seconds - one value for each video
pinSwi= 23
pinOff= 24

#--
import pexpect
from time import sleep
import RPi.GPIO as GPIO
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(pinSwi, GPIO.IN, pull_up_down= GPIO.PUD_UP)
GPIO.setup(pinOff, GPIO.IN, pull_up_down= GPIO.PUD_UP)

def main():
        os.system("clear && tput civis")        #clear and hide cursor
        index= 0        #keeps track of which video to play
        while True:
                sleep(delays[index])
                omx= pexpect.spawn('/usr/bin/omxplayer -rp '+videos[index])
                omx.expect('Video')     #play
                while(GPIO.input(pinSwi)==True):
                        sleep(0.1)
                        if GPIO.input(pinOff)==False:
                                omx.send('q')   #quit
                                os.system("tput cnorm && sudo halt")
                                exit()
                omx.send('q')   #quit
                sleep(0.5)              #safety
                while(GPIO.input(pinSwi)==False):
                        sleep(0.01)
                index= (index+1)%len(videos)

if __name__ == "__main__":
        main()

//----------------------------
//--instructions for installing (you'll need a model B to prepare a sd-card, but then move it over to the model A raspberry)

//--prepare the rpi
* use Pi Filler to transfer 2013-05-25-wheezy-raspbian.img to the sdcard
* put the sdcard in rpi model B
* select 'Expand Filesystem' in and enable SSH under advanced in config menu
* select 'Finish' and reboot
* log in with pi/raspberry
* sudo apt-get update
* sudo apt-get upgrade
* sudo apt-get install python-pexpect avahi-daemon

//--copy files from osx
* open a terminal window on main computer
* cd to folder with videos
* edit the file f0videoplayer.py and select which videos to use
* optionally add delaytimes if some videos should start later
* scp f0videoplayer.py MONITOR01_may2012.mp4 ICHA1.mp4 BLACK.mp4 FLESH.mp4 TESTBILDER.mp4 pi@raspberrypi.local:/home/pi/

//--back to model B
* sudo pico /etc/rc.local
* add the following before the exit line: (sleep 1; python /home/pi/f0videoplayer.py) & # autostart video player
* press ctrl+o to save and ctrl+x to exit
* sudo halt

//--start model A
* take out the sdcard from model B and put it in model A
* connect hdmi or composite video, gpio pins and apply power - the first video should start
* ground pin 23 to cycle through the videos
* ground pin 24 to turn off the computer

//----------------------------
//--useful commands (connect keyboard to rpi model A, type pi/raspberry to log in)
sudo pkill omxplayer.bin     #might need to write this without the terminal being visible

if you get "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!"
ssh-keygen -R raspberrypi.local     #useful for resetting ssh/scp after changing sd cards

it's not pretty but it's working. some day i'll build it in to a real rackmount box.

//--references:
http://www.raspberrypi-spy.co.uk/2013/06/playing-videos-on-the-raspberry-pi-command-line
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=38&t=47520

under the hood changes 2

updating this blog to drupal 7. it is quite different from version 6 and things will be a bit chaotic for a while. sorry that some content here will be unavailable for a few days.

update 130607: fixed the layout and sound files should play again.

arduino livecoding

so here's some more in depth info on my performance at the live.code.festival / algorave in karlsruhe.

being fascinated since long by the sound of serial transmission, i got into trying to make music out of it in some way. by trial-and-error i figured out that if i connect a small speaker to the tx line of an arduino, i could upload programs that send serial data and listen to the sound of it.
it is all very basic: if i make the arduino program send data with delays in between, it play click rhythms. and programs with faster streams of data play tones. more elaborate combinations of delays and patterns of data produce chords, melodies and a variety of noises. so it works like some sort of one-bit music system that is nice and challenging to play with.

the programs i [live]code can look like this...

byte cnt= 0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.write(cnt);
  delay(5);
  cnt++;
}

and the resulting sound is this... (raw and unfiltered)


and a more elaborate program...

byte cnt= 0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  for(int i= 0; i<100; i++) {
    Serial.write(cnt++%5+10);
    delay(random(4));
  }
  for(int i= 0; i<200; i++) {
    Serial.write(cnt++%5+10);
    delay(2);
  }
  for(int i= 0; i<100; i++) {
    Serial.write(cnt++%5+10);
    delay(random(4));
  }
  for(int i= 0; i<100; i++) {
    Serial.write(cnt++%5+10);
    delay(3);
  }
  for(int i= 0; i<100; i++) {
    Serial.write(cnt++%5+10);
    delay(4);
  }
  if(random(2)==0) {
    Serial.begin(random(10000));
  }
}

sounds like this...

and of course the sound of the uploading (verification really) is great in it self. it typically sounds like this... (raw and unfiltered)

i think the uploading sound changes subtly depending on program length and i also guess it will change with different arduino bootloaders and whatever baudrate they are using.

and you also have a bit of control over the timbre of the sounds. certain 8bit numbers are more square-wave like than others e.g. 170 (0b10101010), and 85 (0b01010101) sound more 'clean' and 15 (0b00001111) and 240 (0b11110000) also have a more distinct pitch.
different baudrates have a huge effect on the sound - mainly working as frequency transposition.

but the real fun starts when one connects five arduinos to a mixer and start playing with volumes, panning and filters. by having five arduinos connected to an usb hub while running five copies of the arduino ide software, i can write little programs on the fly that will address the different boards and play different sounds on the tx lines. (listening to the rx line also works but then the upload process fails. it'll require extra circuitry to tap into this data without disrupting the uploading).

the reason i used five arduinos is because that's all i could connect to my laptop (2x usb) with my 4-port usb hub. that in combination with the limitation of computer screen space. it is hard to have more than five arduino ide programs open and visible at the same time.

anyway, as the voltage of the standard arduino is 5v and really a bit too much for audio equipment, i bring this down a bit with a simple voltage divider. i'm using a 10k and a 1k resistor.

here are some pictures of the setup. i'm using the arduino clone red board from sparkfun.

the complete setup (without mixer and laptop)...

one issue with the setup is that one can't trust the arduino ide to remember which serial port it was connected to. so every time i start the program i need to double check that the five arduino ide programs are set to the right arduino board. and as i like to know which board is connected to which mixer channel, i also need to check that and possibly reconnect the sound cables.

live at the live.code.festival in karlsruhe (algorave night 20apr, 2013). five arduino boards all with their serial port (tx line) connected to a mixer (with simple protective circuitry in between). so all sound are generated from what the arduino boards are programmed to transmit serially. note that the sound is heavily distorted. sorry.

ström

since the category 'visuals' is underrepresented in this blog and i don't like to embed video in my standard [html] pages, i thought i'd include this old piece here. this is the shorter abridged version of the full piece. the quality isn't the best - it's pixelated and stuttering. one day i should re-render it in 60fps at a higher resolution. it looks a lot better when running in realtime from a computer.

Ström by Mattias Petersson (music) and Fredrik Olofsson (video) is, in its full version, a 45 minute minimalistic piece for five loudspeakers, live-electronics and live-video, based on an open-minded, artistic approach towards electricity. The piece is an attempt to transfer electric currents via sound to the audience. The five speakers in the surround system struggles to take over the sonic stream like electro-magnets. Sine waves and noise rotates with breakneck speeds around the listeners, tries to charge them with static electricity and, as an ultimate goal, even make them levitate. The video part is in direct connection with the sound and is generated out of five discrete lines – one for each channel in the surround system. The lines are treated in different ways, and as the high voltage builds up in the music they look more and more like electric wires, inflicting each other with violent discharges and eruptions. This version was made for a promotional DVD release on Swedish sound art.

also see here

traer physics library for supercollider

a while ago i started porting the java/processing library TRAER.PHYSICS 3.0 by Jeffrey Traer Bernstein to supercollider. it's a simple and elegant particle system and a physics engine all in one. there are already ports to actionscript3, javascript and c++ (cinder), but i haven't seen anyone working with it in sc yet. so i had a go - both to learn more and to have an alternative to my own physics library quark redUniverse.

it is now finished and released as a quark. this is the initial version and there might still be bugs. i _did see sc crash once in a strange way after spawning lots of particles, so watch out for memory leaks.
to install it run the following code and recompile sc...

Quarks.checkoutAll;
Quarks.install("TraerPhysics");

i also wrote a few simple examples to go along with the helpfiles. here's a screenshot of one...

Pages

Subscribe to f0blog RSS