====== Controlling the LCD Backlight ====== The ComfilePi's LCD backlight is connected to two of the panel PC's internal GPIO pins: One for toggling the backlight on and off, and the other for dimming the backlight. ^ Backlight Control ^ CPi-A/B/S ^ CPi-C ^ | On/Off | GPIO 34 | GPIO 44 | | Dimming | GPIO 31 | GPIO 26 | These GPIO pins can be controlled in multiple ways using file-based interfaces, command line interfaces, or via a programming language. ===== File-Based Interface ===== The backlight control file-based interface was introduced with the ComfilePi Bookworm OS release. File-based interfaces are nice, because they can be utilized from a terminal or any programming language's file I/O features. They also abstract the specific GPIO pins, so the same code can be used on any device model. Write a '1' to ///data/opt/ComfileTech/backlight/control/on// to turn the backlight on. For example ''echo 1 > /data/opt/ComfileTech/backlight/control/on''. Write a '0' to ///data/opt/ComfileTech/backlight/control/on// to turn the backlight off. For example ''echo 0 > /data/opt/ComfileTech/backlight/control/on''. Write a value from '0' to '100' to change the brightness of the backlight (i.e. dim the backlight). For example use ''echo 50 > /data/opt/ComfileTech/baclight/control/brightness'' to change the brightness to 50%. ===== Command Line Interface ===== ==== CPi-A/B/S ==== To turn off the backlight: # using Bookworm pinctrl set 34 op pinctrl set 34 dl # using Bullseye raspi-gpio set 34 op raspi-gpio set 34 dl # using Buster or prior OSes gpio mode 34 output gpio write 34 0 To turn on the backlight: # using Bookworm pinctrl set 34 op pinctrl set 34 dh # using Bullseye raspi-gpio set 34 op raspi-gpio set 34 dh # using Buster or prior OSes gpio mode 34 output gpio write 34 1 ==== CPi-C ==== To turn off the backlight: # using Bookworm pinctrl set 44 op pinctrl set 44 dl # using Bullseye raspi-gpio set 44 op raspi-gpio set 44 dl # using Buster or prior OSes gpio mode 44 output gpio write 44 0 To turn on the backlight: # using Bookworm pinctrl set 44 op pinctrl set 44 dh # using Bullseye raspi-gpio set 44 op raspi-gpio set 44 dh # using Buster or prior OSes gpio mode 44 output gpio write 44 1 ===== Sample C Program ===== The backlight can be controlled in the C programming language using the [[https://abyz.me.uk/rpi/pigpio/pdif2.html|pigpio]] library. #include #define PIN 34 // CPi-A/B/F/S // #define PIN 44 // CPi-C 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); } ===== Adding Screensaver Functionality ===== [[:comfilepi:controlling_the_lcd_backlight:x11_screensaver:index|Synchronizing the Backlight with the X11 Screensaver]] [[:comfilepi:controlling_the_lcd_backlight:non_x11_screensaver:index|Screensaver Functionality In Non-X Programs]] ===== Dimming the Backlight ===== Backlight dimming can be controlled using the PWM on GPIO31 on the CPi-A/B/S or GPIO26 on the CPi-C. Use the pigpio library to adjust the brightness of the backlight. #include #include #include using namespace std; #define PIN 31 // CPi-A/B/F/S // #define PIN 26 // CPi-C int main(int argc, char *argv[]) { int value = stoi(argv[1]); auto instance = pigpio_start(NULL, NULL); set_mode(instance, PIN, PI_OUTPUT); 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 ===== 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. {{ :comfilepi:controlling_the_lcd_backlight:r33.png |}} 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. See [[#dimming_the_backlight|Dimming the Backlight]]. [[comfilepi:index|ComfilePi - Industrial Raspberry Pi Panel PC]]