Below is a simple C++ program designed for the Raspberry Pi 4B to control GPIO pin 12 using hardware PWM with the pigpio library. Before running this program, ensure you have the pigpio library installed on your Raspberry Pi. If not, you can install it by following these steps:
Installing pigpio Library
Update your package list: sudo apt update
Upgrade your system (optional but recommended): sudo apt full-upgrade -y
Install pigpio: sudo apt install pigpio -y
Enable the pigpio daemon to run at startup (if you plan to use it persistently): sudo systemctl enable pigpiod
Start the pigpio daemon (to use it immediately without rebooting): sudo systemctl start pigpiod
C++ Program for Hardware PWM on GPIO 12
This program will set up GPIO pin 12 for hardware PWM, then fade the PWM signal from 0 to 100% and back down to 0% in an infinite loop.
cpp
#include <iostream>
#include <pigpio.h>
#include <unistd.h> // For usleep
#include <signal.h> // For signal handling
// Flag to control the loop
volatile bool running = true;
// Signal handler to stop the program cleanly
void signalHandler(int sig) {
running = false;
}
int main() {
// Register signal handler for clean exit
signal(SIGINT, signalHandler); // Handle Ctrl+C
signal(SIGTERM, signalHandler); // Handle termination signal
// Initialize pigpio
if (gpioInitialise() < 0) {
std::cerr << "pigpio initialization failed." << std::endl;
return 1;
}
// Set GPIO 12 as PWM output using hardware PWM (hardware PWM is enabled by specifying the PWM frequency)
// GPIO 12 is linked to one of the two hardware PWM channels (the other being GPIO 13) on the Raspberry Pi
const int gpio = 12;
const int pwmFrequency = 1000; // Frequency in Hz (e.g., 1000 Hz = 1 kHz)
gpioSetMode(gpio, PI_ALT0); // Set ALT0 function for hardware PWM (Required for hardware PWM)
// Initialize PWM with 0 duty cycle and check for errors
if (gpioHardwarePWM(gpio, pwmFrequency, 0) < 0) {
std::cerr << "Failed to set up hardware PWM." << std::endl;
gpioTerminate();
return 1;
}
int dutyCycle = 0;
const int dutyCycleIncrement = 1;
const int sleepTimeUs = 10000; // 10 ms in microseconds
bool increasing = true;
// Run the PWM fade loop until interrupted
while (running) {
// Set the duty cycle (range: 0-100)
// Duty cycle is in steps of 10000 for 0-100% range
if (gpioHardwarePWM(gpio, pwmFrequency, dutyCycle * 10000) < 0) {
std::cerr << "Error setting PWM duty cycle." << std::endl;
break;
}
// Increment/Decrement duty cycle
if (increasing) {
dutyCycle += dutyCycleIncrement;
if (dutyCycle > 100) { // Max duty cycle is 100%
dutyCycle = 100;
increasing = false;
}
} else {
dutyCycle -= dutyCycleIncrement;
if (dutyCycle < 0) { // Min duty cycle is 0%
dutyCycle = 0;
increasing = true;
}
}
// Sleep for a bit
usleep(sleepTimeUs);
}
// Cleanup
gpioHardwarePWM(gpio, 0, 0); // Stop PWM
gpioTerminate();
std::cout << "Program terminated cleanly." << std::endl;
return 0;
}
Compiling the Program
To compile this program, save it with a .cpp extension (e.g., pwm_example.cpp), then compile it with g++, linking against the pigpio library:
Note: This program runs indefinitely until manually stopped (e.g., with Ctrl+C in the terminal). Also, ensure your user has the necessary permissions to access GPIO; being part of the gpio group is sufficient (achievable with sudo usermod -aG gpio $USER, followed by a logout/login cycle).
Compatibility Note: Ensure your version of pigpio supports the gpioHardwarePWM function. This function is available in recent versions of pigpio, but if you encounter issues, check your library version or update to the latest version.