メインコンテンツへスキップ
見出し画像
Photo bykujiranodanna

RP2040-Zero(マイコンボード) のLチカは一筋縄ではいかない

    こんばんは、実践IOTハウスです

    Amazonでお安いのでポチってしまった
    RP2040-ZeroとV1083がペイントされている
    3個中一つはスルーホールが埋まってPINの半田付けできず😂

    動作確認のLチカが一筋縄ではいかないんです
    と言うのも、これより少し前に購入したRaspberry Pi Pico WH

    Vscodeで初めたPython、すごく簡単にLチカ出来た

    import machine
    import time
    
    led = machine.Pin("LED", machine.Pin.OUT)
    
    while True:
        led.toggle()
        time.sleep(1)

    Webサーバー、ADC、Lチカの並列処理もどきに行き詰まり

    挙句のはてに、jqueryを捨てJavaScriptを再度からやり直す羽目になって

    息抜きでポチってしまったRP2040

    同様の事がこれでも出来ると思ってたのに
    Raspberry Pi Pico WHと違いUF2ファイルの転送も同じ手順では出来ず

    ようやくツールThonnyで

    画像
    Thonny設定
    画像
    MicroPython UF2ファイル転送
    画像
    Raspberry Pi Picoインストール

    さて、LチカだがPIN番を変えても何の反応も無い
    Raspberry Pi Pico WHとは違うLEDが搭載されWS2812と言うものらしい

    ここからコードを拝借して

    # Example using PIO to drive a set of WS2812 LEDs forever
    
    import array, time
    from machine import Pin
    import rp2
    
    # Configure the number of WS2812 LEDs.
    NUM_LEDS = 1
    PIN_NUM = 16
    # brightness = 0.2
    brightness = 0.1
    
    @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
    def ws2812():
        T1 = 2
        T2 = 5
        T3 = 3
        wrap_target()
        label("bitloop")
        out(x, 1)               .side(0)    [T3 - 1]
        jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
        jmp("bitloop")          .side(1)    [T2 - 1]
        label("do_zero")
        nop()                   .side(0)    [T2 - 1]
        wrap()
    
    
    # Create the StateMachine with the ws2812 program, outputting on pin
    #sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
    sm = rp2.StateMachine(0, ws2812, freq=9_000_000, sideset_base=Pin(PIN_NUM))
    
    # Start the StateMachine, it will wait for data on its FIFO.
    sm.active(1)
    
    # Display a pattern on the LEDs via an array of LED RGB values.
    ar = array.array("I", [0 for _ in range(NUM_LEDS)])
    
    ##########################################################################
    def pixels_show():
        dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)])
        for i,c in enumerate(ar):
            r = int(((c >> 8) & 0xFF) * brightness)
            g = int(((c >> 16) & 0xFF) * brightness)
            b = int((c & 0xFF) * brightness)
            dimmer_ar[i] = (g<<16) + (r<<8) + b
        sm.put(dimmer_ar, 8)
        time.sleep_ms(10)
    
    def pixels_set(i, color):
        ar[i] = (color[1]<<16) + (color[0]<<8) + color[2]
    
    while True:
      try:
          print("Red On")
          pixels_set(0, (255,0,0)) # Red
          pixels_show()
          time.sleep(1)
          pixels_set(0, (0,0,0)) #  off
          pixels_show()
          time.sleep(1)
          pixels_set(0, (0,255,0)) # Green
          pixels_show()
          print("Green On")
          time.sleep(1)
          pixels_set(0, (0,0,0)) # off
          pixels_show()
          time.sleep(1)
          pixels_set(0, (0,0,255)) # Blue
          pixels_show()
          print("Blue On")
          time.sleep(1)
          pixels_set(0, (0,0,0)) # off
          pixels_show()
          time.sleep(2)
    
      except KeyboardInterrupt:
          print("Keyboard Interrupt")
          pixels_set(0, (0,0,0)) # off
          pixels_show()
          break

    原本は赤LEDが点灯するのみだが、R→G→B→ループ、Ctrl-cで停止

    画像
    RP2040-Zero V1083 Lチカ

    #iot
    #実践IOTハウス
    #RP2040
    #Lチカ
    #Thonny
    #Vscode

     
     
    実践IOTハウス開発中、 半世紀以上生きて夢中になれるLinuxプログラミング、 日々起きるIOTハウスのワクワクを綴ります 子供が実験や遊び学んだ物が実用化->親が喜ぶ->親が子供を褒める->子供が褒めてもらう為に更に学び工夫する->親が子供を褒める->子供が育つ->社会が育つ

    あなたへのおすすめ