• Week 8 (Session 2)

    Sylvain11 hours ago 0 comments

    I picked up a Rasberry pi at L'Atelier to get familiar with it because it is the first time I use one. I firstly wanted to program in C++ but I really suck at programming so I thought Python was the best language to start. With Python, someone recommend me Thonny as an IDE so I downloaded it then set up the Rasberry pi on the App.

    My first objective was to light a LED only with my computer just to familiarize myself with the link between Python and the Rasberry. I then worked with buttons, a buzzer and a servo (SG90).

    I worked a little bit every day to be able to control the servo with 2 push buttons and this is the code I used :

    from machine import Pin, PWM
    import time
    
    servo = PWM(Pin(15))
    servo.freq(50)
    button1 = Pin(14, Pin.IN, Pin.PULL_UP)
    button2 = Pin(13, Pin.IN, Pin.PULL_UP)
    
    def set_angle(angle):
        min_us = 500
        max_us = 2500
        us = min_us + (angle / 180) * (max_us - min_us)
        duty = int((us / 20000) * 65535)  
        servo.duty_u16(duty)
    
    angle = 90
    
    try:
        while True:
            if button1.value():
                angle = min(angle + 5, 180)
                set_angle(angle)
                time.sleep(0.05)
            
            if button2.value():
                angle = max(angle - 5, 0)
                set_angle(angle)
                time.sleep(0.05)
    except KeyboardInterrupt:
        print("Arrêt du programme")
        set_angle(90)