week 13, apr 17

weekly assignment: add an output device to a microcontroller board and program it to do something

the tools

hardware:
  • raspberry pi, (aka: raspi)
  • servo motor
  • software:
  • the operating system for the raspberry pi: raspbian, a debian linux for raspi's
  • a kernel module developed by adafruit designed to control a servo thru the raspi's GPIO pins
  • putty, a tool to connect to the raspi thru your laptop without the need for a separate keayboard & screen
  • python
  • the idea

    in my final project interactive installation i intend to use a raspberry pi to collect tweets and output sound and voices and maybe lights/video.
    so i wanted to test the raspi and begin to set up my "system" while doing the output device's assignment.
    i decided to try controlling a servo motor with the raspi's GPIO pins (see picture above).

    the program

    the raspi has a set of input and output pons called GPIO (general purpose input output).
    the only GPIO pin that can control a servo is pin 18, also called PWM, for Pulse Width Modulation


    the kernel module i installed (mentioned above) uses a file type of interface, where you control what the output pin 18 and therefore the servo is doing, by reading and writing to special files, see here

    the python program used to control the servo is quite quite short and straightforward, and allows you to control angle and speed.

    # Servo Control
    import time
    def set(property, value):
    try:
    f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
    f.write(value)
    f.close()
    except:
    print("Error writing to: " + property + " value: " + value)


    def setServo(angle):
    set("servo", str(angle))


    set("delayed", "0")
    set("mode", "servo")
    set("servo_max", "180")
    set("active", "1")

    delay_period = 0.01

    while True:
    for angle in range(0, 180):
    setServo(angle)
    time.sleep(delay_period)
    for angle in range(0, 180):
    setServo(180 - angle)
    time.sleep(delay_period)

    installing and connecting everything










    working on the raspi with PUTTY






    the changes to the python scriot were done in the NANO text editor

    delay set to 0.01 sec - slower

    delay set to 0.001 sec - faster

    the results

    here we can see the servo running with the delay set to 0.1 sec

    here we can see the servo running with the delay set to 0.001 sec