Example Projects!

Here are a few examples to get you started

  • Note: Px[y] means Port x, Pin y

Blinky!

The “hello world” of physical computing. Just connect your PiSoC and watch it go! Output is on the on-board LED

1
2
3
4
5
6
7
8
9
from pisoc import *
from time import sleep
PiSoC("PC")

blinky = DigitalPin(12, 0, 'output')

while True:
        blinky.Toggle()
        sleep(1)

Voltmeter

This program demonstrates basic usage of analog.AnalogPin

The program will output the voltage of whatever is connected to P3[0] every .1 seconds.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from pisoc import *
from time import sleep
PiSoC("PC")

voltmeter = AnalogPin(0)

while True:
        voltage = voltmeter.ReadVolts()
        print ("Voltage is %s"%voltage)
        sleep(0.1)

Don’t measure anything more than 5 volts!!

Theremin using PWM and Ultrasonic Range Finding

This will demonstrate some basic use of Ultrasonic Range Finding and tone generation with digital.RangeFinder and digital.PWM

To set this program up, you will want to connect your Ultrasonic Range Finders echo Pin to P12[1], and it’s trigger pin to P12[2]. Also be sure to provide power and ground to the Range Finder from the PiSoC.

Then, connect one end of a piezo buzzer to the PiSoC’s ground, and the other end to P2[1]

1
2
3
4
5
6
7
8
9
from pisoc import *
PiSoC('PC')

ranger = RangeFinder([12,1], [12,2])
piezo = PWM(1)
piezo.Start()
piezo.SetDutyCycle(50) #Max volume
while True:
        piezo.SetFrequency(ranger.readRaw())

Play with some fruit!

This one will be demonstrating use of the Capacitive Sensing libraries with analog.CapSense

Simply connect any kind of fruit (or anything conductive at all) to P4[0], and watch the onboard LED.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from pisoc import *
PiSoC('PC')

touch = CapSense(0, threshold = 6) #raise or lower the threshold depenging on results!
touch.Start()
light = digitalPin(12,0,'output')

while True:
    while touch.isTouched():
        light.Write(1)
    light.Write(0)

Comprehensive usage of digital inputs and outputs

This gives a little bit of a deeper look into the GPIO using digital.DigitalPin

To set this demo up, wire P12[0], P12[1], P12[2], and P12[3] to LEDs in series with a resistor. Also wire the outputs to one of the inputs, in the following way to see accurate data in the terminal: - wire P12[0] to P6[0] - wire P12[1] to P6[1] - wire 12[2] to P6[2] - wire 12[3] to P6[3]

The demo should light the LEDS in forwards order, and then turn off the LEDs in reverse order

The terminal will output the state of the inputs at each step, along with their port/pin number and they should correlate with the LEDs (if wired in the way noted above). Check it out!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from pisoc import *
from time import sleep
PiSoC('PC')

My_outputs = [DigitalPin(12, x, 'output') for x in range(4)]
My_inputs = [DigitalPin(6, x, 'pull_down') for x in range(4)]

while True:
        for i in My_outputs: #Writes each output high, one by one, in forwards order
                i.Write(1)
                for inputs in My_inputs:#Prints the state of each input
                        print ("P%d[%d]:%d" %(inputs.port, inputs.pin, inputs.Read()))
                sleep(0.5)

        for i in reversed(My_outputs):#Turns off the output in reverse order
                i.Write(0)
                for inputs in My_inputs: #Prints the state of each input
                        print ("P%d[%d]:%d" %(inputs.port, inputs.pin, inputs.Read()))
                sleep(0.5)

Servo Controller

This is to show some stuff you can do with digital.Servo

This will use voltage readings from a joystick (or potentiometers), to move two servomotors, possibly connected to a pan-tilt bracket like the one for our face tracker

To make this work, connect your servomotor signal wires to P2[0] and P2[1] (or use the onboard servo connectors) Then connect the analog outputs of your joystick (or potentiometers) to P3[0] and P3[1]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from pisoc import *
PiSoC('PC')

#normally max_angle is 180, but you can change the scale to be whatever is best for the project
Pan = Servo(0, max_angle = 5)   # I chose 5 because the joysticks can output up to 5 Volts
Tilt = Servo(1, max_angle = 5)  # This means that when I get the max volts (5), the servo will be in max position

x_axis = AnalogPin(0)
y_axis = AnalogPin(1)

while True:
        Pan.SetAngle(x_axis.ReadVolts()) #set each servo position to the voltage reading
        Tilt.SetAngle(y_axis.ReadVolts())

NeoPixels

This is just a quick example of some fun with digital.NeoPixelShield Simply connect your NeoPixel Shield to the Arduino headers of your PiSoC, and you’re ready to go!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from pisoc import *
from time import sleep
PiSoC('PC')

pixels = NeoPixelShield()
pixels.Start()
pixels.SetBrightness(3)
colors = [pixels.Green, pixels.Yellow, pixels.Orange, pixels.Red, pixels.Purple, pixels.Blue, pixels.PowderBlue, pixels.White]
while True:
        for column in range(8):
                for row in range(5):
                        pixels.SetPixel(row, column, colors[column])
                sleep(0.01) #so you can see the animation happening

        sleep(0.25)

        for column in reversed(range(1,8)):
                for row in range(5):
                        pixels.SetPixel(row, column, 0)
                sleep(0.01) #so you can see the animation happening

Code author: Brian Bradley <embedit@embeditelectronics.com>

Section author: Brian Bradley <embedit@embeditelectronics.com>