Write your First Python Program for the GrovePi
In this video we walk you through writing your first program in Python for the GrovePi.
Python Documentation
The python library (Available at github) for grove pi has three types of functions:
Basic Arduino Functions
Reads the value from a specified digital pin, either HIGH or LOW.
Parameter:
pin: The Pin number
Return: 1 for HIGH and 0 for LOW
Write a HIGH or a LOW value to a digital pin.
Parameter:
pin: The Pin number
value: 1 for HIGH and 0 for LOW
Return: 1 on success
Configures the specified pin to behave either as an input or an output.
Parameter:
pin: the number of the pin whose mode you wish to set
mode: INPUT, OUTPUT
Return: 1 on success
Reads the value from the specified analog pin.
Parameters
pin: the number of the analog input pin to read from (0,1 and 2)
Returns: 0 to 1023
Writes an analog value (PWM wave) to a pin
Parameters
pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on).
Return: 1 on success
Grove Specific functions
Read the temperature from the analog Grove Temperature Sensor http://www.seeedstudio.com/depot/Grove-Temperature-Sensor-p-774.html
Parameter:
pin: the number of the analog input pin to read from (0,1 and 2)
Return: temp in float
Read the distance reading from the Grove Ultasonic ranger http://www.seeedstudio.com/depot/Grove-Ultrasonic-Ranger-p-960.html
Parameter:
pin: pin on which the Ultrasonic ranger is connected
Return: distance in cm
read the x,y,z value from the accelerometer http://www.seeedstudio.com/depot/Grove-3Axis-Digital-Accelerometer15g-p-765.html?cPath=25_26
return: an array of 3 values [x,y,z] -32 to 32 (-1.5g to +1.5g)
Read the time from Grove RTC http://www.seeedstudio.com/depot/Grove-RTC-p-758.html?cPath=25_30
Return: 8 byte array [hour,min,sec,month,dayOfMonth,year,dayOfMonth,dayOfWeek] or junk value if RTC not connected
Parameter:
pin: pin on which the DHT sensor is connected
module_type:
0- DHT11
1- DHT22
2- DHT21
3- AM2301
Return: array of 2 floats [temp,humidity]
Private functions (should not be used directly)
Write a block of data to address using I2C interface
Parameter:
address: address of the I2C device (0x04 GrovePi)
block: block of data in bytes to be sent
Return: positive value on success, -1 on failure
Read a block of data from address using I2C interface
Parameter:
address: address of the I2C device (0x04 GrovePi)
Return: block of data read on success, -1 on failure
Read a byte of data from address using I2C interface
Parameter:
address: address of the I2C device (0x04 GrovePi)
Return: byte of data read on success, -1 on failure
Have a question or a problem? Post it on the forums and we’ll help you out.