1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電子工作メモ - ADコンバータ

Last updated at Posted at 2024-03-21

ADコンバータ

  • ポテンショメータ: 半固定抵抗

  • ADC(analog digital converter)
    計測したアナログ信号をデジタル信号に変換する
    2つのキーポイント

    • 分解能(resolution)
    • チャンネル(channel)
  • RaspberyPi の分解能

    • 12ビット($2^{12} = 4096 $)の分解能がある
      ex) 3.3Vの範囲の電圧の場合、3.3 / 4096 = 0.8mV 単位で分割される

    • しかし、MicroPythonの内部的なプロセスが16ビット($ 2^{16} = 65536 $)の分解能のため、0 - 65535 に分割される
      ex) 3.3Vの範囲の電圧の場合、3.3 / 65536 = 0.05mV 単位で分割される

      $ADCの値 = \frac{V}{3.3} \cdot 65536 $

ピン

  • ポテンショメータ
    • 1: 3V3

    • 2: GND

    • 3: GP26

      09.1_AnalogRead
      from machine import ADC, Pin
      import time
      
      adc = ADC(26)
      try:
          while True:
              adcValue = adc.read_u16()
              voltage = adcValue / 65535.0 * 3.3
              print(f"ADC Value: {adcValue} Voltage: {voltage}V")
              time.sleep(0.1)
      except:
          pass
      

ポテンショメータとLED

ピン

  • ポテンショメータ

    • 1: 3V3
    • 2: GND
    • 3: GP26
  • LED

    • アノード: 220Ω + GP15

    • カソード: GND

      10.1_Soft_LED
      from machine import ADC, Pin, PWM
      import time
      
      adc = ADC(26)
      pwm = PWM(Pin(15))
      pwm.freq(1000)
      
      try:
          while True:
              adcValue = adc.read_u16()
              pwm.duty_16(adcValue)
              time.sleep(0.1)
      except:
          pwm.deinit()
      

Soft Colorful Light

ピン

  • ポテンショメータ1

    • 1: 3V3
    • 2: GND
    • 3: GP26
  • ポテンショメータ2

    • 1: 3V3
    • 2: GND
    • 3: GP27
  • ポテンショメータ3

    • 1: 3V3
    • 2: GND
    • 3: GP28
  • RGBLED

    • R: 220Ω + GP15
    • G: 220Ω + GP14
    • B: 220Ω + GP13
    • アノードコモン: 3V3
    10.2_Soft_Colorful_Light
    from machine import ADC, Pin, PWM
    import time
    
    adc_list = [ADC(26), ADC(27), ADC(28)]
    
    pwm_list = [PWM(Pin(gpno)) for gpno in [13, 14, 15]]
    [pwm.freq(1000) for pwm in pwm_list]
        
    try:
        while True:
            for adc, pwm in zip(adc_list, pwm_list):
                pwm.duty_u16(65535 - adc.read_u16())
            time.sleep(0.1)
    except:
        for pwm in pwm_list:
            pwm.deinit()
    

Sofr Rainbow Light

ピン

  • ポテンショメータ

    • 1: 3V3
    • 2: GND
    • 3: GP26
  • NeoPixel

    • VCC: 3V3

    • GND: GND

    • IN : GP15

    • OUT: N/A

      10.3_Soft_Rainbow_Light
      from machine import Pin, ADC
      from neopixel import myNeopixel
      import time
      
      red   = 0
      green = 0
      belu  = 0
      np = myNeopixel(8, 15)
      adc0 = ADC(26)
      
      # 255を2色で分ける
      def wheel(pos):
          global red, green, blue
          WheelPos = pos % 255
      
          # 3分割しているので1/3の値を求める
          one_third = (WheelPos % 85)
          big   = one_third * 3
          small = 255 - big
      
          # 赤<=>緑
          if WheelPos < 85:
              red   = small
              green = big
              blue  = 0
          # 緑<=>青
          elif 85 <= WheelPos < 170:
              red   = 0
              green = small
              blue  = big
          # 青<=>赤
          else :
              red   = big
              green = 0
              blue  = small
      
      np.brightness(20)
      while True:
          adcValue = adc0.read_u16() // 257
          for j in range(0, 8):
              wheel(adcValue + j*255 // 8)
              np.set_pixel(j, red, green, blue)
              np.show()
          time.sleep_ms(100)
      
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?