Last active
December 16, 2024 16:39
-
-
Save addieljuarez/3686d0a42a7a197cb48f2d81a8a8b4ca to your computer and use it in GitHub Desktop.
primero ejercicios de raspberry pico
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # brilla el led de la placa | |
| from machine import Pin | |
| led = Pin(25, Pin.OUT) | |
| led.toggle() | |
| # parpadea el led de la placa | |
| # blink | |
| from machine import Pin, Timer | |
| led = Pin(25, Pin.OUT) | |
| timer = Timer() | |
| def blink(timer): | |
| led.toggle() | |
| timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink) | |
| # parpadea un led con resistencia de 330 ohm | |
| # https://raspberrypi.cl/wp-content/uploads/2022/06/Rpi-pico-paso-a-paso23.png | |
| from machine import Pin, Timer | |
| led = Pin(15, Pin.OUT) | |
| timer = Timer() | |
| def blink(timer): | |
| led.toggle() | |
| timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink) | |
| # Controlar un led mediante un pulsador | |
| # https://raspberrypi.cl/wp-content/uploads/2022/06/Rpi-pico-paso-a-paso25.png | |
| from machine import Pin | |
| import time | |
| led = Pin(15, Pin.OUT) | |
| button = Pin(14, Pin.IN, Pin.PULL_DOWN) | |
| while True: | |
| if button.value(): | |
| led.toggle() | |
| time.sleep(0.5) | |
| # controla el brillo con un PWM | |
| # https:/ es.wikipedia.org/wiki/Pulse-width_modulation | |
| # https://raspberrypi.cl/wp-content/uploads/2022/06/Rpi-pico-paso-a-paso25.png | |
| from machine import Pin, PWM | |
| from time import sleep | |
| pwm = PWM(Pin(15)) | |
| pwm.freq(1000) | |
| while True: | |
| for duty in range(65025): | |
| pwm.duty_u16(duty) | |
| sleep(0.0001) | |
| for duty in range(65025, 0, -1): | |
| pwm.duty_u16(duty) | |
| sleep(0.0001) | |
| # cmuestra el valor del potenciometro | |
| # https://raspberrypi.cl/wp-content/uploads/2022/06/Rpi-pico-paso-a-paso26.png | |
| from machine import ADC, Pin | |
| from time import sleep | |
| adc = ADC(Pin(26)) | |
| while True: | |
| print(adc.read_u16()) | |
| sleep(1) | |
| # controla el led con un potenciometro | |
| # https://raspberrypi.cl/wp-content/uploads/2022/06/Rpi-pico-paso-a-paso26.png | |
| from machine import Pin, PWM, ADC | |
| pwm = PWM(Pin(15)) | |
| adc = ADC(Pin(26)) | |
| pwm.freq(1000) | |
| while True: | |
| duty = adc.read_u16() | |
| pwm.duty_u16(duty) | |
| # prender led 25 con un pulsador | |
| # https://raspberrypi.cl/wp-content/uploads/2023/02/Captura-de-pantalla-2023-02-28-a-las-15.26.03-600x857.png | |
| from machine import Pin | |
| from time import sleep_ms | |
| LED = Pin(25, Pin.OUT) | |
| Button = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| while True: | |
| LED.value(Button.value()) | |
| # interrupcion IRQ(Interrupt Request) con un pulsador | |
| # https://raspberrypi.cl/wp-content/uploads/2023/02/Captura-de-pantalla-2023-02-28-a-las-15.26.03-600x857.png | |
| from machine import Pin | |
| from time import sleep_ms | |
| LED = Pin(25, Pin.OUT) | |
| Button = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| LedState = False | |
| def ButtonIRQHandler(pin): | |
| print(pin) | |
| global LedState | |
| print(LedState) | |
| LedState = not LedState | |
| Button.irq(trigger=Pin.IRQ_RISING, handler=ButtonIRQHandler) | |
| while True: | |
| LED.value(LedState) | |
| # un buzzer con 2 botones | |
| # https://raspberrypi.cl/wp-content/uploads/2023/02/WhatsApp-Image-2023-02-28-at-10.20.15.jpeg | |
| from machine import Pin, PWM | |
| from time import sleep_ms | |
| ButtonA = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| ButtonB = Pin(1, Pin.IN, Pin.PULL_DOWN) | |
| Buzzer = PWM(Pin(15)) | |
| Buzzer.duty_u16(3276) | |
| Frecuency = 1000 | |
| def ButtonIRQHandler (pin): | |
| global Frecuency | |
| print(Frecuency) | |
| if pin == ButtonA: | |
| if Frecuency < 2000: | |
| Frecuency += 50 | |
| elif pin == ButtonB: | |
| if Frecuency > 50: | |
| Frecuency -= 50 | |
| ButtonA.irq(trigger=Pin.IRQ_RISING, handler=ButtonIRQHandler) | |
| ButtonB.irq(trigger=Pin.IRQ_RISING, handler=ButtonIRQHandler) | |
| while True: | |
| Buzzer.freq(Frecuency) | |
| # mas interrupciones | |
| # interrupciones de 2 leds con 2 botones | |
| # https://raspberrypi.cl/wp-content/uploads/2023/03/exp5-600x618.png | |
| # https://raspberrypi.cl/wp-content/uploads/2023/02/WhatsApp-Image-2023-02-28-at-11.45.37-1024x382.jpeg | |
| from machine import Pin | |
| LED1 = Pin(25, Pin.OUT) | |
| LED2 = Pin(10, Pin.OUT) | |
| ButtonA = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| ButtonB = Pin(1, Pin.IN, Pin.PULL_DOWN) | |
| ledState1 = True | |
| ledState2 = False | |
| def ButtonAIRQHandler(Pin): | |
| global ledState1 | |
| print(Pin) | |
| if Pin == ButtonA: | |
| ledState1 = not ledState1 | |
| def buttonBIRQHandler(Pin): | |
| global ledState2 | |
| print(Pin) | |
| if Pin == ButtonB: | |
| ledState2 = not ledState2 | |
| ButtonA.irq(trigger= Pin.IRQ_RISING, handler= ButtonAIRQHandler) | |
| ButtonB.irq(trigger= Pin.IRQ_RISING, handler= buttonBIRQHandler) | |
| while True: | |
| LED1.value(ledState1) | |
| LED2.value(ledState2) | |
| # manejo de threads | |
| # https://raspberrypi.cl/wp-content/uploads/2023/03/exp6-679x1024.png | |
| # https://raspberrypi.cl/wp-content/uploads/2023/02/WhatsApp-Image-2023-02-28-at-11.56.03.jpeg | |
| from machine import Pin, PWM | |
| from time import sleep_ms | |
| from _thread import start_new_thread | |
| ButtonA = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| Red = Pin(10, Pin.OUT) | |
| Yellow = Pin(11, Pin.OUT) | |
| Green = Pin(12, Pin.OUT) | |
| Buzzer = PWM(Pin(15)) | |
| Buzzer.duty_u16(0) | |
| Frecuency = 1000 | |
| Beeping = False | |
| def Beep (): | |
| global Beeping | |
| OnTime = 500 | |
| print('Start beeping thread') | |
| while Beeping: | |
| Buzzer.duty_u16(32767) | |
| sleep_ms(OnTime) | |
| Buzzer.duty_u16(0) | |
| sleep_ms(1000 - OnTime) | |
| print('End Beeping Thread') | |
| def ButtonAIRQHandler(pin): | |
| global Beeping | |
| if Beeping == False: | |
| print ('Start beeping') | |
| Beeping = True | |
| start_new_thread(Beep, ()) | |
| else: | |
| Beeping = False | |
| print('Stop Beeping') | |
| ButtonA.irq (trigger = Pin.IRQ_RISING, handler = ButtonAIRQHandler) | |
| while True: | |
| Red.toggle() | |
| sleep_ms(100) | |
| Yellow.toggle() | |
| sleep_ms(100) | |
| Green.toggle() | |
| sleep_ms(100) | |
| Buzzer.freq (1000) | |
| # Cross Request with threads | |
| # https://raspberrypi.cl/wp-content/uploads/2023/03/exp7-602x1024.png | |
| from machine import Pin, PWM | |
| from time import sleep_ms, sleep | |
| import _thread | |
| ButtonA = Pin(0, Pin.IN, Pin.PULL_DOWN) | |
| Red = Pin(10, Pin.OUT) | |
| Yellow = Pin(11, Pin.OUT) | |
| Green = Pin(12, Pin.OUT) | |
| PedestrianRed = Pin(6, Pin.OUT) | |
| PedestrianWait = Pin(7, Pin.OUT) | |
| PedestrianGreen = Pin(8, Pin.OUT) | |
| Buzzer = PWM(Pin(15)) | |
| Buzzer.duty_u16(0) | |
| Frequency = 1000 | |
| CrossRequested = False | |
| def PedestrianCross(): | |
| global CrossRequested | |
| PedestrianRed(0) | |
| PedestrianGreen(1) | |
| PedestrianWait(0) | |
| OnTime = 50 | |
| print('Beeping') | |
| for Beeping in range (10): | |
| print(Beeping) | |
| Buzzer.duty_u16(32767) | |
| sleep_ms(OnTime) | |
| Buzzer.duty_u16(0) | |
| sleep_ms(1000 - OnTime) | |
| print('End beep thread') | |
| PedestrianRed(1) | |
| PedestrianGreen(0) | |
| CrossRequested = False | |
| def ButtonIRQHandler (pin): | |
| global CrossRequested | |
| if CrossRequested == False: | |
| print('Button Presses') | |
| CrossRequested = True | |
| PedestrianWait.value(1) | |
| ButtonA.irq(trigger= Pin.IRQ_RISING, handler= ButtonIRQHandler) | |
| while True: | |
| if CrossRequested == True: | |
| _thread.start_new_thread(PedestrianCross, ()) | |
| while CrossRequested: | |
| sleep(1) | |
| else: | |
| Yellow.value(1) | |
| sleep(1) | |
| Red.value(0) | |
| Yellow.value(1) | |
| Green.value(1) | |
| sleep(2) | |
| Yellow.value(1) | |
| Green.value(0) | |
| sleep(1) | |
| Red.value(1) | |
| Yellow.value(0) | |
| sleep(2) | |
| Buzzer.freq(Frequency) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment