serialBegin — Open a serial port.


Plugin opcode in serial.

Open a serial port for arduino.


iPort serialBegin SPortName [, ibaudRate]


SPortName -- port name number

ibaudrate -- serial speed, defaulting to 9600 bps.


Here is an example of the serialBegin opcode. It uses the file serialBegin.csd.

Example 974. Example of the serialBegin opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

; Select audio/midi flags here according to platform
-n  ;;;no output
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o serialBegin.wav -W ;;; for file output any platform

sr  = 44100
ksmps = 500 ; the default krate can be too fast for the arduino to handle
nchnls_i = 1
0dbfs  = 1

instr 1

iPort serialBegin "/dev/cu.usbmodemfa131", 9600			;connect to the arduino with baudrate = 9600

kGain init 16							;read our knob value
kVal serialRead iPort
if (kVal != -1) then
    kGain = kVal/128

aSig in								;get our audio input and get its rms
kRms rms aSig*kGain

kRms = kRms*kRms*255						;scale the rms to a good value for the LED and send it out
serialWrite iPort, (kRms < 255 ? kRms : 255)			;must be in range: 0-255

f 1 0 1024 10 1 1 1 1 1 1

i 1 0 200

This is the matching Arduino code :

void setup() {
  // enable serial communication

  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);

void loop() {
  // only do something if we received something (this should be at csound's k-rate)
  if (Serial.available()) {
    // set the brightness of LED (connected to pin 9) to our input value
    int brightness =;
    analogWrite(9, brightness);

    // while we are here, get our knob value and send it to csound
    int sensorValue = analogRead(A0);
    Serial.write(sensorValue/4); // scale to 1-byte range (0-255)

See Also

non-MIDI Devices


Author: Matt Ingalls

New in version 5.14