Trouble modifying code for the Display pack 2.0"

I am working with the display pack2. I am trying to output to the onboard LED when button A is pressed but it’s not working, I have tried a number of variations and so far this doesn’t bring up any errors but it doesn’t work. The code is below, and I have ‘#’ where there are changes .Any suggestions welcome Nick `

# This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed.

# I have modified the code to ouput to the on board LED  but its not working yet.......led1 . 


from machine import Pin, Timer
import time
import utime
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P4
from pimoroni import RGBLED
led1_Pin =Pin(25,Pin.OUT)   #output to onboard led hopefully
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)

display.set_backlight(0.5)
display.set_font("bitmap8")
led = RGBLED(6,7,8)
led1_Pin = Pin(25,Pin.OUT)   #onboard led  
timer = Timer()

def blink(timer):
    led1.toggle()

button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)


# sets up a handy function we can call to clear the screen
def clear():
    display.set_pen(BLACK)
    display.clear()
    display.update()


# set up
clear()

while True:
    if button_a.read():                                   # if a button press is detected then...
        clear()                                           # clear to black
        display.set_pen(WHITE)                            # change the pen colour
        display.text("Button A pressed", 10, 10, 240, 4)  # display some text on the screen
        display.update()                                  # update the display
        led.set_rgb(255,255,255)
        led1_Pin.value(1)                                 # onboard led
        led1_Pin.toggle()                                 # onboard led    
        time.sleep(1)                                     # pause for a sec
        clear()                                           # clear to black again
       elif button_b.read():
        clear()
        display.set_pen(CYAN)
        display.text("Button B pressed", 10, 10, 240, 4)
        display.update()
        led.set_rgb(0,255,255)
        time.sleep(1)
        clear()
    elif button_x.read():
        clear()
        display.set_pen(MAGENTA)
        display.text("Button X pressed", 10, 10, 240, 4)
        display.update()
        led.set_rgb(255,0,255)
        time.sleep(1)
        clear()
    elif button_y.read():
        clear()
        display.set_pen(YELLOW)
        display.text("Button Y pressed", 10, 10, 240, 4)
        display.update()
        led.set_rgb(255,255,0)
        time.sleep(1)
        clear()
    else:
        display.set_pen(GREEN)
        display.text("Press any button!", 10, 10, 240, 4)
        display.update()
        led.set_rgb(0,255,0)
    time.sleep(0.1)  # this number is how frequently the Pico checks for button presses

Are you using a Pico or a Pico W? If you have a Pico W the onboard LED is wired up slightly differently - try:

led = machine.Pin('LED', machine.Pin.OUT)

I’m using pico w and I have used the code the way you have supplied it and also as led1 or LED1 , but if I use 'led ’ the rest of the code confuses the onboard led as the RGBLED as the names are the same(I can’t have 2 “led” with the same name if they are in different physical places and do different jobs. Thank you for the suggestion. See code below. first the error statement

Traceback (most recent call last):
File “”, line 14, in
ValueError: unknown named pin “LED1”


This example shows you a simple, non-interrupt way of reading Pico Display’s buttons with a loop that checks to see if buttons are pressed.

I have modified the code to ouput to the on board LED but its not working yet…led1 .

from machine import Pin, Timer
import time
import utime
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P4
from pimoroni import RGBLED
#from pimoroni import LED
#led1 = LED1
led1 = machine.Pin(‘LED1’,machine.Pin.OUT) #output to onboard led hopefully

We’re only using a few colours so we can use a 4 bit/16 colour palette and save RAM!

display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)

display.set_backlight(0.5)
display.set_font(“bitmap8”)
led = RGBLED(6,7,8)
led1.off() # = Pin(25,Pin.OUT) #onboard led
led1.on()
timer = Timer()

def blink(timer):
led1.toggle()

button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)

sets up a handy function we can call to clear the screen

def clear():
display.set_pen(BLACK)
display.clear()
display.update()

set up

clear()

while True:
if button_a.read(): # if a button press is detected then…
clear() # clear to black
display.set_pen(WHITE) # change the pen colour
display.text(“Button A pressed”, 10, 10, 240, 4) # display some text on the screen
display.update() # update the display
led.set_rgb(255,255,255)
led1.on() #Pin.value(1) # onboard led
led1.toggle() # onboard led
time.sleep(1) # pause for a sec
clear() # clear to black again

elif button_b.read():
    clear()
    display.set_pen(CYAN)
    display.text("Button B pressed", 10, 10, 240, 4)
    display.update()
    led.set_rgb(0,255,255)
    time.sleep(1)
    clear()
elif button_x.read():
    clear()
    display.set_pen(MAGENTA)
    display.text("Button X pressed", 10, 10, 240, 4)
    display.update()
    led.set_rgb(255,0,255)
    time.sleep(1)
    clear()
elif button_y.read():
    clear()
    display.set_pen(YELLOW)
    display.text("Button Y pressed", 10, 10, 240, 4)
    display.update()
    led.set_rgb(255,255,0)
    time.sleep(1)
    clear()
else:
    display.set_pen(GREEN)
    display.text("Press any button!", 10, 10, 240, 4)
    display.update()
    led.set_rgb(0,255,0)
time.sleep(0.1)  # this number is how frequently the Pico checks for button presses

Are you planning on using both LED’s, the onboard and display pack LED?
If not just remark out the Display pack LED.
#led = RGBLED(6,7,8)
and use
led = machine.Pin('LED', machine.Pin.OUT)

Code tags are the “preformatted text” function ,click the </> button in the forum editor.

For the Pico W it’s as follows.

import machine
led = machine.Pin("LED", machine.Pin.OUT)
led.off()
led.on()

EDIT: What happens if you edit it like so?
rgb_led = RGBLED(6,7,8)
led = machine.Pin('LED', machine.Pin.OUT)

Still no luck , I might be making mistakes in my coding.
See code below…Thank you

# This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed.

# I have modified the code to ouput to the on board LED  but its not working yet.......led1 . 


from machine import Pin, Timer
import time
import utime
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_P4
from pimoroni import RGBLED
#from pimoroni import LED        when compiled it comes back with"cannot import LED"
led = machine.Pin('LED',machine.Pin.OUT)   #output to onboard led hopefully

# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)

display.set_backlight(0.5)
display.set_font("bitmap8")
#led = RGBLED(6,7,8)
# led.off()                # = Pin(25,Pin.OUT)   #onboard led  
# led.on()
# timer = Timer()

# def blink(timer):
#     led.toggle()

button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)


# sets up a handy function we can call to clear the screen
def clear():
    display.set_pen(BLACK)
    display.clear()
    display.update()


 #set up
clear()

while True:
    if button_a.read():                                   # if a button press is detected then...
        clear()                                           # clear to black
        display.set_pen(WHITE)                            # change the pen colour
        display.text("Button A pressed", 10, 10, 240, 4)  # display some text on the screen
        display.update()                                  # update the display
        rgb_led =  RGBLED(6,7,8)                                 # FROM ALPHANUMERIC
        led = machine.Pin('LED',machine.Pin.OUT)                 # FROM ALPHANUMERIC 
        #led.off()                                         # onboard led  
        #led.on()
        #led.set_LED()                            # CODE WILL UP LOAD BUT CREATES AN ERROR WHEN BUTTON "A" PUSHED AttributeError: 'Pin' object
                                                #   has no attribute 'set_LED'
        #led.set_rgb(255,255,255)
        time.sleep(1)                                     # pause for a sec
        clear()                                           # clear to black again
        
        
    elif button_b.read():
        clear()
        display.set_pen(CYAN)
        display.text("Button B pressed", 10, 10, 240, 4)
        display.update()
        #led.set_rgb(0,255,255)
        time.sleep(1)
        clear()
    elif button_x.read():
        clear()
        display.set_pen(MAGENTA)
        display.text("Button X pressed", 10, 10, 240, 4)
        display.update()
        #led.set_rgb(255,0,255)
        time.sleep(1)
        clear()
    elif button_y.read():
        clear()
        display.set_pen(YELLOW)
        display.text("Button Y pressed", 10, 10, 240, 4)
        display.update()
        #led.set_rgb(255,255,0)
        time.sleep(1)
        clear()
    else:
        display.set_pen(GREEN)
        display.text("Press any button!", 10, 10, 240, 4)
        display.update()
        #led.set_rgb(0,255,0)
    time.sleep(0.1)  # this number is how frequently the Pico checks for button presses
    

If you already have
rgb_led = RGBLED(6,7,8)
and
led = machine.Pin('LED',machine.Pin.OUT)
defined above the while true, you don’t need to do it again. As far as I know anyway.

Try this,

if button_a.read():                                   # if a button press is detected then...
        clear()                                           # clear to black
        display.set_pen(WHITE)                            # change the pen colour
        display.text("Button A pressed", 10, 10, 240, 4)  # display some text on the screen
        display.update()                                  # update the display
        #rgb_led =  RGBLED(6,7,8)                                 # FROM ALPHANUMERIC
        #led = machine.Pin('LED',machine.Pin.OUT)                 # FROM ALPHANUMERIC 
        #led.off()                                         # onboard led  
        led.on()
        #led.set_LED()                            # CODE WILL UP LOAD BUT CREATES AN ERROR WHEN BUTTON "A" PUSHED AttributeError: 'Pin' object
                                                #   has no attribute 'set_LED'
        #led.set_rgb(255,255,255)
        time.sleep(1)                                     # pause for a sec
        clear() 

If I get a chance later on I’ll dig my display pack out and a Pico W, and try some code.
My only spare Pico W doesn’t have any headers soldered on it. No big deal I was going solder some on anyway, just needed a nudge.

You’ll need to edit it for a Display Pack 2 I only had a spare (smaller) Display Pack
The onboard LED will blink 3 times, then you’ll get the press a button screen.
Pressing A will turn on the onboard LED.
Pressing B will turn it off.
Pressing X will turn the RGB LED on
Pressing Y will turn it off again.

import time
import machine
led = machine.Pin("LED", machine.Pin.OUT)

time.sleep(1)
led.on()
time.sleep(1)
led.off()
time.sleep(1)
led.on()
time.sleep(1)
led.off()
time.sleep(1)
led.on()
time.sleep(1)
led.off()


from pimoroni import RGBLED
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4

rgb_led = RGBLED(6, 7, 8)
rgb_led.set_rgb(0, 0, 0)

# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)

display.set_backlight(0.5)
display.set_font("bitmap8")

button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)


# sets up a handy function we can call to clear the screen
def clear():
    display.set_pen(BLACK)
    display.clear()
    display.update()
clear()
while True:
# set up

    if button_a.read():                                   # if a button press is detected then...
        clear()                                           # clear to black
        display.set_pen(WHITE)                            # change the pen colour
        display.text("Button A pressed", 10, 10, 240, 4)  # display some text on the screen
        display.update()                                  # update the display
        led.on()
        #rgb_led.set_rgb(255, 255, 255)
        time.sleep(1)                                     # pause for a sec
        
        clear()                                           # clear to black again
    elif button_b.read():
        clear()
        display.set_pen(CYAN)
        display.text("Button B pressed", 10, 10, 240, 4)
        display.update()
        #rgb_led.set_rgb(0, 0, 0)
        led.off()
        time.sleep(1)
        clear()
    elif button_x.read():
        clear()
        display.set_pen(MAGENTA)
        display.text("Button X pressed", 10, 10, 240, 4)
        display.update()
        rgb_led.set_rgb(255, 255, 255)
        time.sleep(1)
        clear()
    elif button_y.read():
        clear()
        display.set_pen(YELLOW)
        display.text("Button Y pressed", 10, 10, 240, 4)
        display.update()
        rgb_led.set_rgb(0, 0, 0)
        time.sleep(1)
        clear()
    else:
        display.set_pen(GREEN)
        display.text("Press any button!", 10, 10, 240, 4)
        display.update()
    time.sleep(0.1)  # this number is how frequently the Pico checks for button presses

Hi Kerry
Thanks for your reply. I will check it out in the morning , it’s getting a bit late now
Cheers
Nick

Brilliant ,It works,thank you for your efforts .I have to admit I did make a mistake in downloading the uf2 file . I downloaded the uf2 for a standard Pico not a Pico W, so I have corrected that . I simply didn’t see the uf2 for the Pico w.

Thats a very easy mistake to make. Nice to hear you got it working. =)