PIIO Simple Example

Example of the PIIO board functionality.
I use the command line in the example below but it is not meant that it should be used like that.
This may interrupt the steelsquid daemon so for example the power off button stop working.
You should use it from example kiss_expand.py running inside the steelsquid daemon.
Look in the file steelsquid_piio.py if you want to know more about the python code.

On the PIIO board the GPIO pins has the number 1,2,3... You can use that numbering or the normal Raspberrt Pi GPIO numbering.
The numbering looks like: 1_14, 2_15, 3_18...   Before the underscore is the PIIO board pin nr and after is the Raspberry Pi GPIO.

Also see the advanced example and extend functionality how to use PIIO board with the modules.

Enable the PIIO board in Steelsquid Kiss OS

To enable the board just execute this in the command line.
steelsquid piio-on

Or you can do it from the synchronization program by pressing e and then the number in front of kiss_piio.

Power on and power off the PIIO board

By pressing the blue POWER_ON button the board will power n and the system boots.
By pressing the POWER_OFF button or execute a command you can shutdown and power off the board.
When the Raspberry Pi starts to shutdown a signal is sent to a timer circuit and after a configurable time the power is shutoff.
You can change the delay by turning the trim DELAY_POW_OFF (from 2 to about 60 seconds).
  • Test with different values of DELAY_POW_OFF
  • Press the POWER_OFF button
  • Command line
    piio shutdown
  • Python code
    steelsquid_piio.shutdown().

Main in Voltage

If you connect a OLED LCD to the Board the voltage will show like in the picture bellow.
If you change it up and down it should change on the LCD to.
    Read the main in voltage of the PIIO board
    • Command line
      piio volt
    • Python code
      voltage = steelsquid_piio.volt()
    You can also listen for changes and make a method execute like this.
    • Command line
      piio volt_event
    • Python code
      def on_change(voltage):
          sys.stdout.write("Voltage="+str(voltage).ljust(20) + "\r")
          sys.stdout.flush()
      steelsquid_piio.volt_event(on_change)
    Also check the PIIO class in modules/ (see kiss_expand.py).
    • on_voltage_change(voltage)
      Will execute when voltage is changed 0.1V
    • on_low_bat(voltage)
      Will execute when voltage i lower than parameter: 
      voltage_warning
      Change the parameter like this.
    • When voltage is under the parameter voltage_poweroff the system will shutdown,

    INFO Button

    There is one yellow INFO button.
    Use this to read current status.
    • Python code
      clicked_or_not = steelsquid_piio.info()
    You can also listen for changes and make a method execute like this.
    • Python code
      def on_change(status):
          print status
      steelsquid_piio.info_event(on_change)
    You can also listen for click and make a method execute like this.
    • Python code
      def on_change():
          print "Clicked"
      steelsquid_piio.info_click(on_change)
    Also check the PIIO class in modules/ (see kiss_expand.py).
    • on_button_info()
      Will execute when info button is clicked...

    Buttons

    There is 6 buttons BUTTON_1 to 6 on the board to use for input.
    Use this to read current status.
    • Command line
      piio button 1
    • Python code
      clicked_or_not = steelsquid_piio.button(1)
    You can also listen for changes and make a method execute like this.
    • Command line
      piio button_event 1
    • Python code
      def on_change(button_nr, status):
          print "Changed"
      steelsquid_piio.button_event(1, on_change)
    You can also listen for click and make a method execute like this.
    • Command line
      piio button_click 1
    • Python code
      def on_change(button_nr):
          print "Clicked"
      steelsquid_piio.button_click(1, on_change)
    Also check the PIIO class in modules/ (see kiss_expand.py).
    • on_button(button_nr)
      Will execute when button 1 to 6 is pressed

    DIP switches

    There is 6 DIP switches SWITCH_1 to 6 on the board to use for input.
    Use this to read current status.
    • Command line
      piio switch 1
    • Python code
      on_or_off = steelsquid_piio.switch(1)
    You can also listen for changes and make a method execute like this.
    • Command line
      piio switch_event 1
    • Python code
      def on_change(dip_nr, status):
          print "Changed"
      steelsquid_piio.switch_event(1, on_change)
    Also check the PIIO class in modules/ (see kiss_expand.py).
    • switch(dip_nr, status)
      Execute when switch 1 to 6 is is changed on the PIIO board.

    LED

    There is 6 LED_1 to 6 on the board to use for output.
    • Command line
      piio led 1 on
      piio led 1 off
    • Python code
      steelsquid_piio.led(1, True)
      steelsquid_piio.led(1, False)

    Buzzer

    There is also a buzzer (BUZ) to make a sound.
    • Command line
      piio buz on
      piio buz off
    • Python code
      steelsquid_piio.buz(True)
      steelsquid_piio.buz(False)

    LCD

    Print text to the OLED LCD
    • Command line (This will show for 5 seconds then the old text with IP will show.)
      piio lcd "A text"
    • Python code (ADD: number_of_seconds = 3 if you want the IP to cum back)
      steelsquid_piio.lcd("A text")

    Raspberry Pi GPIO (Set and get state)

    Connect GPIO pin 1 to 3V3 or GND and test to read state from a pin.
    • Command line
      piio gpio_get 1
    • Python code
      true_or_false = steelsquid_piio.gpio_get(1)
    Test to set a gpio state.
    Connect  GPIO pin 1 to a LED long leg and the other leg to GND.
    • Command line
      piio gpio_set 1 on
      piio gpio_set 1 off
    • Python code
      steelsquid_piio.gpio_set(1, True)
      steelsquid_piio.gpio_set(1, False)

    Raspberry Pi GPIO (Listen for changes)

    You can listen for changes on a PIN and make a method execute on change.
    Connect a wire to GPIO pin 1 execute the command below. 
    Then connect it to GND and 3V3 to test the events.
    • Command line
      piio gpio_event 1
    • Python code
      def method_to_execute(pin, status): 
          steelsquid_utils.log("PIN: " + str(pin) + "=" + str(status))
      steelsquid_piio.gpio_event(1, method_to_execute)
    You can also listen for button click on a PIN and make a method execute on change.
    Connect a button to GPIO pin 1 and GND and execute the following command.
    • Command line
      piio gpio_click 1
    • Python code
      def method_to_execute(pin): 
          steelsquid_utils.log("Click PIN: " + str(pin))
      steelsquid_piio.gpio_click(1, method_to_execute)

    Use 5V with Raspberry Pi GPIO

    By adding a jumper to GPIO_5V_ON you can use 5V on pin 1 to 8.
    Do not use the same pin that you use for 3.3V instead use the pins named GPIO_5V.
    Make the same test as previous example but use 5V.
    But you can not test the button click because of the pull down will not work as expected.
    You can still use the GPIO when the GPIO_5V_ON is enabled.
    But the 3.3v pins will act a little different.
    If you connect the 5V pins to 5V the signal will be high until you connect it to GND.
    So if you use a pull down resistor on the 3.3V pins and dont connect the pin to gnd the signal will be hight if the last signal to the GPIO_5V was high. And vice versa if the last GPIO_5V was low (The pull down resistor will have no effect).

    Extra GPIO (Set and get state)

    The 8 extra 3.3v gpios can be used in the same way as the Raspberry Pi GPIO.
    Connect XGPIO pin 1 to 3V3 or GND and test to read status of the pin.
    • Command line
      piio xgpio_get 1
    • Python code
      true_or_false = steelsquid_piio.xgpio_get(1)
    Test to set a gpio state.
    Connect  XGPIO pin 1 to a LED long leg and the other leg to GND.
    • Command line
      piio xgpio_set 1 on
      piio xgpio_set 1 off
    • Python code
      steelsquid_piio.xgpio_set(1, True)
      steelsquid_piio.xgpio_set(1, False)

    Extra GPIO (Listen for changes)

    The 8 extra 3.3v gpios can be used in the same way as the Raspberry Pi GPIO.
    You can listen for changes on a PIN and make a method execute on change.
    Connect a wire to XGPIO pin 1 execute the command below. 
    Then connect it to GND and 3V3 to test the events.
    • Command line
      piio xgpio_event 1
    • Python code
      def method_to_execute(pin, status): 
          steelsquid_utils.log("PIN: " + str(pin) + "=" + str(status))
      steelsquid_piio.xgpio_event(1, method_to_execute)
    You can also listen for button click on a PIN and make a method execute on change.
    Connect a button to XGPIO pin 1 and GND and execute the following command.
    • Command line
      piio xgpio_click 1
    • Python code
      def method_to_execute(pin): 
          steelsquid_utils.log("Click PIN: " + str(pin))
      steelsquid_piio.xgpio_click(1, method_to_execute)

    High current/voltage PINS

    There are 8 high current and voltage pins named POWER. They can handle up to 50V and 500mA.
    Test it by connecting something that needs more current for example a dc motor.
    Set the voltage to what the dc motor needs on V_ADJ.
    Then connect the DC motor to V_ADJ and pin 1 on POWER and test it.
    • Command line
      piio power 1 on
      piio power 1 off
    • Python code
      steelsquid_piio.power(1, True)
      steelsquid_piio.power(1, False)

    ADC

    You can read analog voltage in from 7 ADC pins.
    Connect  a ADC pin to GND and 3.3 volt to test.
    You can also test to connect it to V_ADJ, but remember not to input more than 3.3V or you can destroy it.
    • Command line
      piio adc 1
    • Python code
      voltage = steelsquid_piio.adc(1)
    You can also listen for changes and make a method execute like this.
    • Command line
      piio adc_event 1
    • Python code
      def on_change(channel, voltage):
          sys.stdout.write("X="+str(voltage).ljust(20) + "\r")
          sys.stdout.flush()
      steelsquid_piio.adc_event(1, on_change)

    DAC

    Write analog out from the 4 DAC pins (0 to 3.3v).
    Connect  a LED long leg to DAC pin 1 and the short leg to GND
    You set the voltage on all 4 pins at the same time: 0 to 4095  (0V to 3.3V).
    Experiment with diferent values to see the led lit up.
    • Command line
      piio dac 1000 0 0 0
    • Python code
      voltage = steelsquid_piio.dac(1000 0 0 0)

    Servo controller

    You can control 12 servos (SERVO_1 to 12) from the PIIO board.
    Depending on the voltage of the servos set the voltage to the SRV pins.
    Or just put a jumper and use 5V (Works on most servos).

    Then connect a servo to servo port 1 and test it.
    The position if the servo can be 0 to 255
    • Command line
      piio servo 1 3
      piio servo 1 240
    • Python code
      steelsquid_piio.servo(1, 3)
      steelsquid_piio.servo(1, 244)

    PWM

    You can control 4 PWM pins on the PIIO board.
    The PWM pins use the same curcit that control the DC motors.
    You can not use the PWM and MOTOR controller at the same time.
    Connect LED long leg to PWM pin 1 and the other leg to GND
    The value can be from 0 to 1023. Depending on the value the LED should 
    illuminate different amount.
    • Command line
      piio pwm 1 500
      piio servo 1 1020
    • Python code
      steelsquid_piio.pwm(1, 500)
      steelsquid_piio.pwm(1, 1020)

    DC motors

    You can control the speed and direction of 2 DC motors (MOTOR_1 and MOTOR_2).
    The motors use the same curcit as the PWM pins.
    You can not use the PWM and MOTOR controller at the same time.
    Depending on the voltage of the motors you can set the working voltage for the motors on the V_M pin.
    Or add a jumper to use main in voltage of the board.
    You set the speed and direction of both motors with the same command.
    -1023 = Full speed in reverse
    0 = Stop
    1023 = Full speed forwards
    • Command line
      piio motor 500 -1000
      piio motor 0 0
    • Python code
      steelsquid_piio.motor(500, -1000)
      steelsquid_piio.motor(0, 0)

    Gyro and Accelerometer

    If you have the mpu-6050 connected to the bottom of PIIO board you can read gyro and accelerometer data.
    First test to read the data.
    Experiment by tilting and moving the board.
    • Command line
      piio movement
      piio rotation
    • Python code
      (x, y, z) = steelsquid_piio.movement()
      (x, y) = steelsquid_piio.rotation()
    You can also listen for changes and make a method execute like this.
    • Command line
      piio movement_event
      piio rotation_event
    • Python code
      def movement_change(x, y, z):
          sys.stdout.write("X="+str(x).ljust(8)+"Y="+str(y).ljust(8)+"Z="+str(z).ljust(8) + "\r")
          sys.stdout.flush()
      steelsquid_piio.movement_event(movement_change)
      def rotation_changed(x, y):
          sys.stdout.write("X="+str(x).ljust(20)+"Y="+str(y).ljust(20) + "\r")
          sys.stdout.flush()
      steelsquid_piio.rotation_event(rotation_changed)
    Also check the PIIO class in modules/ (see kiss_expand.py).
    • on_movement(x, y, z)
      Execute if Geeetech MPU-6050 is connected and the device is moved.
    • on_rotation(x, y)
      Execute if Geeetech MPU-6050 is connected and the device is tilted.