In pulse width modulation average voltage is proportional to duty cycle.
Average voltage is proportional to duty cycle.

WiringPi and Pulse-Width-Modulation with Raspberry Pi

by Lewis Loflin

YouTube video see Pulse-Width-Modulation with Raspberry Pi.

WiringPi allows one use Arduino type programming with the Raspberry Pi GPIO. Here I'll explore how this is used and how it differs from Arduino.

I'll connect Raspberry Pi to a LED using pulse-width modulation to control intensity.

PWM in Arduino is 8-bit in WiringPi it's 10-bit. If one uses Raspberry Pi 2 or 3 pins 1 and 26 are the same thing - PWM on 1 is also on 26! WiringPi is native to the newest version of Raspbian. One must be root to use. Remember Arduino is 5-volts and RPi is 3.3-volts!

This is written in C and has to be compiled. I suggest using Geany under Linux. Get it "sudo apt-get install geany-plugins".

WiringPi was developed by Gordon Henderson.

Pulse-Width Modulation Tutorial YouTube
Pulse-Width Modulation Tutorial

#include <stdio.h>
#include <wiringPi.h>
#include <stdlib.h>
// only P1 and P26 works for PWM0
// at the same time!

int main (void)   {
  int num ;
  printf ("Raspberry Pi wiringPi PWM test program\n") ;
  if (wiringPiSetup () == -1)
    exit (1) ;

  // set pin 1 to PWM
  pinMode (1, PWM_OUTPUT) ;

  for (;;)   {

    for (num = 0 ; num < 1024 ; ++num)
    {
      pwmWrite (1, num) ;
      delay (1) ;
    }
    delay(1000);
    for (num = 1023 ; num >= 0 ; --num)
    {
      pwmWrite (1, num) ;
      delay (1) ;
    }

    delay(1000);
  } // end for

  return 0 ;
}



Loading

YouTube Videos:
MCP4725 12-Bit DAC Interface to Raspberry Pi
ADS1115 4-Channel ADC Uses I2C with Raspberry Pi
Interface I2C LCD to Raspberry Pi in C
Pulse-Width-Modulation with Raspberry Pi
Using Geany Text editor C Programming
Raspberry Pi Blink Demo
MAX6675 Raspberry Pi Demo

Videos:
Raspberry Pi, Arduino, and Learning Linux
Raspberry PI Arduino Advanced Interface
Tkinter with Raspberry Pi and PCF8591 AD-DA Sensor

Linux Videos

Live Linux Distro for Using Printer Port with Electronics
Using the powerful Rox-Filer system in Linux
Use FEH under Linux for a Wallpaper Setter
How to create Symbolic links in Linux

 


donate