arduino-emulator
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Servo Class Reference

Arduino-compatible Servo library for Raspberry Pi. More...

#include <Servo.h>

Public Member Functions

 Servo ()
 Default constructor. Uses the global RPI GPIO instance.
 
 Servo (HardwareGPIO_RPI &gpio)
 Constructor with explicit HardwareGPIO_RPI instance.
 
uint8_t attach (int pin)
 Attach the servo to a GPIO pin using default pulse range.
 
uint8_t attach (int pin, int min, int max)
 Attach the servo to a GPIO pin with custom min/max pulse widths.
 
bool attached () const
 Check if the servo is attached to a pin.
 
void detach ()
 Detach the servo: stop PWM and release the pin.
 
int read () const
 Read the current servo position in degrees (0-180).
 
int readMicroseconds () const
 Read the current pulse width in microseconds.
 
void write (int value)
 Write a position to the servo.
 
void writeMicroseconds (int us)
 Write a pulse width directly in microseconds.
 

Detailed Description

Arduino-compatible Servo library for Raspberry Pi.

Uses the Linux sysfs PWM interface via HardwareGPIO_RPI. Supports hardware PWM pins: 12, 13, 18, 19 (BCM numbering).

The servo period is fixed at 50 Hz (20 ms). Pulse widths in the range SERVO_DEFAULT_MIN_US-SERVO_DEFAULT_MAX_US map to 0-180 degrees.

Note
Call gpio.begin() on your HardwareGPIO_RPI instance before attaching any servos.

Usage:

HardwareGPIO_RPI gpio;
gpio.begin();
Servo myServo(gpio);
myServo.attach(18);
myServo.write(90);
Arduino-compatible Servo library for Raspberry Pi.
Definition Servo.h:60

Member Function Documentation

◆ attach() [1/2]

uint8_t Servo::attach ( int  pin)
inline

Attach the servo to a GPIO pin using default pulse range.

Parameters
pinGPIO pin number (BCM), must be a hardware PWM pin (12,13,18,19).
Returns
0 on success.

◆ attach() [2/2]

uint8_t Servo::attach ( int  pin,
int  min,
int  max 
)
inline

Attach the servo to a GPIO pin with custom min/max pulse widths.

Parameters
pinGPIO pin number (BCM), must be a hardware PWM pin (12,13,18,19).
minMinimum pulse width in microseconds (corresponds to 0 degrees).
maxMaximum pulse width in microseconds (corresponds to 180 degrees).
Returns
0 on success.

◆ write()

void Servo::write ( int  value)
inline

Write a position to the servo.

Parameters
valueAngle in degrees (0-180), or pulse width in us if > 200.

◆ writeMicroseconds()

void Servo::writeMicroseconds ( int  us)
inline

Write a pulse width directly in microseconds.

Parameters
usPulse width in microseconds.

The documentation for this class was generated from the following file: