I have created a module, (based on the WT_LCDBacklight module), and added 2 additional methods to allow turning off and on the LCD Backlight from Ardupy Micropython.
The code in the module is as follows:
#define LCD_BACKLIGHT (72Ul)
void common_hal_lcdbacklight_turn_off(abstract_module_t *self) // function to turn off the backlight
{
pinMode(LCD_BACKLIGHT, OUTPUT);
digitalWrite(LCD_BACKLIGHT, 0);
}
void common_hal_lcdbacklight_turn_on(abstract_module_t *self) // function to turn on the backlight
{
pinMode(LCD_BACKLIGHT, OUTPUT);
digitalWrite(LCD_BACKLIGHT, 1);
}
I then create and flash a build and I can see the methods but the digitalWrite doesn’t change the LCD_BACKLIGHT to 0 and therefore it never turns off the LCD Backlight.
I have added the following printf before and after the method and sure enough it is always 1:
printf("common_hal_lcdbacklight_turn_off BEFORE -> LCD_BACKLIGHT = %u, current = %d\n\n", LCD_BACKLIGHT, digitalRead(LCD_BACKLIGHT));
pinMode(LCD_BACKLIGHT, OUTPUT);
digitalWrite(LCD_BACKLIGHT, 0);
delay(10);
printf("common_hal_lcdbacklight_turn_off AFTER -> LCD_BACKLIGHT = %u, current = %d\n\n", LCD_BACKLIGHT, digitalRead(LCD_BACKLIGHT));
Any ideas why the digitalWrite has no effect on the 72Ul pin?
First of all, you cannot use 72Ul, PinMode and digitalWrite on ArduPy, because they are not included in the ArduPy APIs.
You could check these documents for further clarification:
Define a pin:
p3 = Pin(3, Pin.OUT) // Setting Pin 3 as an output pin
Set the pin to HIGH:
p3.on()
Set the pin to LOW:
p3.off()
I would suggest you to check out this playlist to learn more about ArduPy:
Even though 72Ul pin is defined for Arduino, you cannot directly use that pin with ArduPy because it is not defined within ArduPy APIs.
However, there is a way to achieve what you have been trying before. That is by using the following ArduPy library:
This is used to control the brightness of the LCD. But however, you can use it to turn the LCD ON and OFF as well by using the following codes.
Turn OFF LCD:
backlight.setBrightness(0)
Turn ON LCD:
backlight.setBrightness(100)
Here is an example sketch for your reference. This would turn ON/OFF LCD every 2 seconds.
# include libraries
from arduino import wt_lcdbacklight
from machine import LCD
import time
lcd = LCD() # initialize LCD
backlight = wt_lcdbacklight() # initialize backlight
def main(): # main function
lcd.fillScreen(lcd.color.RED) # fill background
while True: # while loop
backlight.setBrightness(0) # turn off the LCD backlight
time.sleep(2) # delay for 2 seconds
backlight.setBrightness(100) # turn on the LCD backlight
time.sleep(2)
if __name__ == "__main__": # check whether this is run from main.py
main() # execute function
Please try the above methods and share your updates.
Hi Lakshantha,
We currently use a slightly modified WTbacklight module that I have modified to simply be “turnOff” and “turnOn” functions setting as you say, the backlight to 0 or 100 accordingly.
I originally in this C library tried to manipulate 72Ul because of the reason you say that Ardupy hardware ID’s only go up to 64 but as I say, although it accepted the call it had no effect from out c module.
Anyway, I will leave the code as adjusting the backlight to 0 or 100 until Ardupy catches up and exposes 72Ul as an allowable hardware id or the Ardupy LCD class exposes a working “Turn Off” and “Turn On” method which would be useful.