User Tools

Site Tools

한국어

comfilepi:controlling_the_lcd_backlight:index

Controlling the LCD Backlight

The ComfilePi's LCD backlight is connected to GPIO 34. The backlight can be turned on or off via the command line or via a programming language.

Command Line Interface

To turn off the backlight:

gpio mode 34 output
gpio write 34 0

To turn on the backlight:

gpio mode 34 output
gpio write 34 1

Sample C Program

The backlight can be controlled in the C programming language using the pigpio library.

#include <pigpiod_if2.h>
 
#define PIN 34
 
void backlight_on()
{
    auto instance = pigpio_start(NULL, NULL);
 
    set_mode(instance, PIN, PI_OUTPUT);
    gpio_write(instance, PIN, 1);
 
    pigpio_stop(instance);
}
 
void backlight_off()
{
    auto instance = pigpio_start(NULL, NULL);
 
    set_mode(instance, PIN, PI_OUTPUT);
    gpio_write(instance, PIN, 0);
 
    pigpio_stop(instance);
}

Note: The version of the pigpio library that ships with the ComfilePi's OS image does not recognize the Raspberry Pi 3 Compute Module on which the ComfilePi is built. Therefore, functions like set_mode will return PI_NOT_PERMITTED. To resolve this issue, simply modify /lib/systemd/system/pigpiod.service with the following line to set the GPIO mask:

ExecStart=/usr/bin/pigpiod -l -x0x00FFFFFFFFFFFF

Hardware Revisions

v2.0+

CPi_A070WR (S/N : A01001-17-7-11 ~ A01001-17-11-91)

CPi_A102WR (S/N : A01003-17-7-1 ~ A01003-17-10-26)

The initial ComfilePi that was produced did not populate R33 on the main board's PCB. On those models, switching the LCD backlight on or off will not work unless R33 is populated. Hardware revision v2.0 populates R33 with a 22 Ohm resistor, so switching the backlight on or off will work out of the box.

To test which hardware revision you have, simply run the commands illustrated above and see if they work.

v2.1+ (Refer to hardware version printed on sticker)

Hardware revision v2.1 connects the backlight to GPIO 31 which supports PWM. Use the pigpio library to adjust the brightness of the backlight.

#include <pigpiod_if2.h>
#include <iostream>
#include <string>
 
using namespace std;
 
#define PIN 31
 
int main(int argc, char *argv[])
{
   int value = stoi(argv[1]);
 
   auto instance = pigpio_start(NULL, NULL);
 
   set_PWM_frequency(instance, PIN, 200);
 
   set_PWM_dutycycle(instance, PIN, value);
 
   pigpio_stop(instance);
}

Compile and run:

g++ backlight.cpp -lpigpiod_if2 -lpthread -o backlight

./backlight 0    # minimum brightness
./backlight 128  # medium brightness
./backlight 255  # maximum brightness
comfilepi/controlling_the_lcd_backlight/index.txt · Last modified: 2018/07/13 13:23 by COMFILE Technology