Skip to content

Pulse Width Modulation

Tushar Semwal edited this page Nov 8, 2017 · 4 revisions
  1. 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: An integer value (max 255) that will be converted to 8-bit integer and written to GPIO pins 0 (lower bits) to 7 (higher bits). **Note: As a second arguement, an underscore '_' is provided.
  2. 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.
  3. pwmSetRange(T): This function sets the range register in the PWM generator. The default value is 1024.
    • T: Integer value for the range.
  4. pwmSetClock(+T): This function sets the divisor for the PWM clock.
    • T: Integer value for the clock.
  5. 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: Returns either 1 or 2.
  6. 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.
  7. 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:

  • Connect a LED (make sure to use a resistor of atleast 100 ohms) to the Raspberry Pi at GPIO pins 1 (positive terminal of led) and 0 (negative terminal).
  • 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 minimum to maximum and back to the minimum.
    ?- start.
Clone this wiki locally