- Install latest version of Raspbian on Raspberry pi. The OS can be downloaded from the following link .
- Install swi-prolog on the system(as root user) using the command:
% sudo apt-get install swi-prolog
- Download/Copy the WiringPi-Prolog tar file(provided here) onto the Raspberry pi.
- Unzip the WiringPi-Prolog zip file using the command:
$ unzip <file_name>
- Check the swi-prolog installation directory. Generally the location is ‘/usr/lib/swi-prolog’. To check otherwise use the following commands:
$ swipl
?- file_search_path(swi, X). //returns installation directory.
- Copy the wipi folder from the unzipped folder(step 4) to the folder returned in step 5.
- Get out of superuser if still in it.
- Go to your working directory(folder where you will be working with your project files).
- Copy the ‘platform_pi.pl’(provided here) file in this directory.
- Now start prolog with sudo permission(very important):
$ sudo swipl
- Consult the
platform_pi.pl
using:?- consult('platform_pi.pl').
This will load all of the Tartarus predicates. - Execute the command:
?- start_peripherals.
This will start the peripheral Interface. Make sure no errors occur during this step. - Now the peripheral interface is ready, and you can use WiringPi Prolog predicates to control the GPIO pins.
- Set the mode of a pin to input or output using the pinMode command:
Syntax: pinMode(pin,mode)- pin: GPIO pin number according to WiringPi.
- mode: INPUT(0) or OUTPUT(1) mode or PWM_OUTPUT(2).
- Use digitalWrite command to write a value to the particular pin.
Syntax: digitalWrite(pin,value)- pin: GPIO pin number according to WiringPi.
- value: set value HIGH(1) or LOW(0) for the pin.
- Use digitalRead command to read the value that a particular pin is set to
Syntax: digitalRead(pin,X)- pin: GPIO pin number according to WiringPi.
- X: variable in which the response of the command is stored.
Example for LED:
- Connect positive of led to GPIO pin 1(see WiringPi pin mapping) along with a resistance in series.
- Connect negative of led to GPIO pin 0(see WiringPi pin mapping).
- Write the following code to light up the LED.
?- pinMode(0,0).
--set pinmode of pin 0 to 0 for output.?- pinMode(1,0).
--set pinmode of pin 1 to 0 for output.?- digitalWrite(0,0).
--set value of pin 0 to 0 for LOW/ground.?- digitalWrite(1,1).
--set value of pin 1 to 1 for HIGH/(3.3v). - This will light up the LED.
- To turn it off use the digitalWrite command to write value 0 to pin 1
- Connect the I2C device to the Raspberry pi.
- Check if the device is connected properly using the command:
$ sudo i2cdetect -y 1
It should show something like:
- The address of the I2C device that we interact with is the one that appears on the table in the above image.
- The commands that can be used to interact with the I2C device are:
- wiringPiI2CSetup(addr,Fd): To setup a connection with the device.
- Addr: The address of the device obtained in step 2. Example: 0x68 (refer image in step 2).
- Fd: The value returned by the setup command for furthur referencing to the device. - wiringPiI2CWriteReg8(Fd,reg,val): To write a value in a particular register on the device.
- Fd: The value of Fd obtained from the setup command.
- reg: Address of the register to which the value is to be written.
- val: The value to be written to the particular register (reg). Note: wiringPiI2CWriteReg16(Fd,reg,Val) is also available - wiringPiI2CReadReg8(Fd,reg,Val): To read the output from a particular register on the device.
- Fd: The value of Fd obtained from the setup command.
- reg: Address of the register from which the value is to be read.
- Val: The value stored in the register returned by the command. Note: wiringPiI2CReadReg16(Fd,reg,Val) is also available
Example for IMU:
- Follow the first three steps of I2C to connect the IMU to Raspberry pi.
- Now once we get the address we type the following commands:
?- wiringPiI2CSetup(0x68, Fd).
--returns the value of Fd(4 in this case) to be used in later commands.?- wiringPiI2CWriteReg8(4,0x6b,0x00).
--sets a value to particular registers (refer to IMU datasheet) to awaken the device.?- wiringPiI2CWriteReg8(4,0x6c,0x00).
--sets a value to particular registers to awaken the device.?- wiringPiI2CWriteReg8(4,0x74,0x00).
--sets a value to particular registers to awaken the device.?- wiringPiI2CReadReg8(4,0x43,T).
--returns the value of stored in 0x43 register.
- wiringPiI2CSetup(addr,Fd): To setup a connection with the device.
The commands are mentioned below:
- delay(t): Used to apply a delay of t milliseconds.
- t: Time in milliseconds.
- delayMicroseconds(t): Used to apply a delay of t microseconds.
- t: Time in microseconds.
- millis(T): It returns a number(T) representing the number of milliseconds since the program called one of the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after 49 days.
- T: Time in milliseconds returned by the function.
- micros(T): It returns a number representing the number of microseconds since the program called one of the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
- T: Time in microseconds returned by the function.
Example for LED blinking:
- Commect a LED to Raspberry Pi at GPIO pins 1(HIGH) and 0(LOW).
- Write the following code in a prolog file(in the working directory).
Blink_Led - Now start prolog with sudo permission(very important):
$ sudo swipl
- Consult the
platform_pi.pl
using:?- consult('platform_pi.pl').
This will load all of the Tartarus predicates. - Execute the command:
?- start_peripherals.
- Execute the following command to see the LED blink and the output of millis command on terminal.
?- start.
- digitalWriteByte(t,_): This function writes the 8-bit byte supplied to the first 8 GPIO pins.It takes two write operations to the Pi’s GPIO hardware.
- t: number that will be converted to 8 bit integer and written to GPIO pins 0(lower bits) to 7(higher bits).
- pwmSetMode(t): The PWM generator can run in 2 modes – “balanced” and “mark:space”.
- t: 0 for "balanced" mode and 1 for "mark:space" mode.
- pwmSetRange(t): This function sets the range register in the PWM generator. The default is 1024.
- t: Integer value for the range.
- pwmSetClock(t): This function sets the divisor for the PWM clock.
- t: Integer value for the clock.
- piBoardRev(T): This function returns the board revision of the Raspberry Pi. It will be either 1 or 2. Some of the BCM_GPIO pins changed number and function when moving from board revision 1 to 2, so if you are using BCM_GPIO pin numbers, then you need to be aware of the differences.
- T: Return valiable which is either 1 or 2.
- pwmWrite(pin,value): This function is used to give pwm output values to a PWM pin.
- pin: The pin number to which the pwm output is to be written. On raspberry pi the gpio pin 1 is used for pwm output. Other pins may also be available depending on versions of pi.
- value: Value to be written to pin ranging from 0 to 1024.
- wpiPinToGpio(pin,T): This function returns the BCM_GPIO pin number of the supplied wiringPi pin. It takes the board revision into account.
- pin: wiringPi pin number.
- T: Value of the BCM_GPIO pin number is returned in this variable.
- physPinToGpio(pin,T): This function returns the BCM_GPIO pin number of the supplied physical pin on the P1 connector.
- pin: wiringPi pin number.
- T: Value of the physical pin number is returned in this variable.
Note: If you want to use a Pin as a PWM pin, set its mode to 2 using the command pinMode.
For e.g., pinMode(1,2)
This will allow you to use Pin 1 as a PWM output pin (0 is for input, 1 for output and 2 for PWM).
Example for LED blinking:
- Commect a LED to Raspberry Pi at GPIO pins 1(HIGH) and 0(LOW).
- Write the following code in a prolog file(in the working directory).
PWM_led - Now start prolog with sudo permission(very important):
$ sudo swipl
- Consult the
platform_pi.pl
using:?- consult('platform_pi.pl').
This will load all of the Tartarus predicates. - Execute the command:
?- start_peripherals.
- Execute the following command to see the LED intensity change from minimun to maximum and back to minimum.
?- start.