이 튜토리얼에서는 SBC에 가장 일반적인 유형의 모터를 연결하는 방법을 살펴봅니다. 또한 몇 가지 예시를 통해 이를 제어하는 방법도 보여드립니다. 가장 일반적인 모터 유형은 다음과 같습니다:
- DC 모터는 저렴하고 제어하기 쉬우며, 단 하나의 전력 출력만 필요로 하고 이는 PWM 신호로 활성화할 수 있습니다.
- 서보 모터는 DC 모터와 유사하지만, 폐쇄 루프 제어를 형성하기 위한 소형 전자 장치와 내부 전위차계가 포함되어 있어 회전 각도를 제어할 수 있습니다. 제어 역시 PWM을 통해 이루어지지만, 이 경우 제어 신호는 저전력이며 전력단이 서보 모터 자체에 내장되어 있습니다.
- 스테퍼 모터는 여러 개의 권선을 가지고 있으며 제어를 위해 시퀀스가 필요하지만, 그 대가로 권선의 극성이 반전될 때마다 한 스텝씩 전진하여 뛰어난 정밀도를 제공합니다.
1. DC 모터
DC 모터를 제어하려면 다음 다이어그램에 표시된 대로 SBC에 연결합니다:


모터의 몇 가지 매개변수, 구체적으로 인가 전압과 모터 속도의 관계 또는 Kv를 알아야 합니다.

Dc_Motor 클래스는 50Hz의 PWM으로 제어되는 DC 모터를 나타냅니다. 볼 수 있듯이 전원 공급 전압, Kv 상수, 사용할 핀과 같은 일부 매개변수를 제공해야 합니다. 그 결과로 set_voltage와 set_speed 두 가지 함수를 얻게 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Dc_Motor: FREQ_Hz = 50 def __init__( self, pin, vm, kv ): self.vm = vm self.kv = kv self.pin = pin self.dout = sbc.Dout() self.dout.set_pwm( self.pin, self.FREQ_Hz, 0 ) self.set_voltage( 0 ) def set_voltage( self, v ): pulse_percent_on = 100*v/self.vm self.dout.set_pwm( self.pin, self.FREQ_Hz, 100-pulse_percent_on ) def set_speed( self, rpm ): self.set_voltage( rpm/self.kv ) |
사용 예시는 다음과 같습니다:
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Dc_Motor( "DOUT3", vm=12, kv=0.5 )
>>> print( "Voltage control" )
Voltage control
>>> motor.set_voltage( 6 )
>>> time.sleep( 1 )
>>> motor.set_voltage( 0 )
>>> print( "Speed control" )
Speed control
>>> motor.set_speed( 100 )
>>> time.sleep( 1 )
>>> motor.set_speed( 0 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Servo_Motor: FREQ_Hz = 50 DUTY_MIN_ms = 0.5 DUTY_MAX_ms = 2.5 def __init__( self, pin ): self.pin = pin self.dout = sbc.Dout() self.dout.set_pwm( self.pin, self.FREQ_Hz, 0 ) def set_angle( self, angle_deg ): duty_ms = self.DUTY_MIN_ms + (self.DUTY_MAX_ms-self.DUTY_MIN_ms)*angle_deg/180 pulse_percent_on = self.FREQ_Hz*duty_ms/10 self.dout.set_pwm( self.pin, self.FREQ_Hz, 100-pulse_percent_on ) |
사용 예시:
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Servo_Motor( "DOUT3" )
>>> print( "Set angle to 90 degrees" )
Set angle to 90 degrees
>>> motor.set_angle( 90 )
>>> time.sleep( 1 )
>>> print( "Set angle to 0 degrees" )
Set angle to 0 degrees
>>> motor.set_angle( 0 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Stepper_Motor: def __init__( self, pins ): self.pins = pins self.dout = sbc.Dout() self.state = 0 self.steps = 0 self.dir = 1 self.timer = machine.Timer( -1 ) def step( self, direction ): if( self.state == 0 ): self.dout.write( self.pins[0], 1 ) self.dout.write( self.pins[1], 1 ) self.dout.write( self.pins[2], 0 ) self.dout.write( self.pins[3], 0 ) elif( self.state == 1 ): self.dout.write( self.pins[0], 0 ) self.dout.write( self.pins[1], 1 ) self.dout.write( self.pins[2], 1 ) self.dout.write( self.pins[3], 0 ) elif( self.state == 2 ): self.dout.write( self.pins[0], 0 ) self.dout.write( self.pins[1], 0 ) self.dout.write( self.pins[2], 1 ) self.dout.write( self.pins[3], 1 ) elif( self.state == 3 ): self.dout.write( self.pins[0], 1 ) self.dout.write( self.pins[1], 0 ) self.dout.write( self.pins[2], 0 ) self.dout.write( self.pins[3], 1 ) self.steps += direction self.state = (self.state + direction) & 0x03 def set_speed( self, steps_s, direction ): if( steps_s and direction ): self.timer.init( period=int(1000/steps_s), mode=machine.Timer.PERIODIC, callback=lambda tmr: self.step(direction) ) else: self.timer.deinit() |
마지막으로, 사용 예시입니다:
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Stepper_Motor( ["DOUT1", "DOUT2", "DOUT3", "DOUT4"] )
>>> print( "Step by step" )
Step by step
>>> for i in range( 100 ):
... motor.step( 1 )
... time.sleep_ms( 10 )
>>> print( "Continuous" )
Continuous
>>> motor.set_speed( 100, -1 )
>>> time.sleep( 1 )
>>> motor.set_speed( 0, 0 )
및




