exercise
압력센서로 DC모터 속도 조절해 봅시다
material
DC motor(gearhead)
H-bridge(SN754410NE or L293D)
Force Sensitive Resistor(UST-SNR-FSR no.402)
10K Ω resistor
circuit
![](http://mimlab.net/wp-content/uploads/2022/01/17.DC모터-속도조절하기-1024x519.png)
code
int pwmpin = 5; //voltage input on pin 9
int motor1pin = 6;
int motor2pin = 7;
int sensorpin = 2; //sensor input on analog pin 2
int val = 0;
void setup() {
Serial.begin(9600);
pinMode(motor1pin, OUTPUT);
pinMode(motor2pin, OUTPUT);
}
void loop() {
digitalWrite(motor1pin, LOW); //this start up the motor
digitalWrite(motor2pin, HIGH); //in one direction
val = analogRead(sensorpin); //read sensor value
Serial.println(val); //confirm val thru serial monitor
val = map(val, 0, 1023, 0, 255); //map it to 0~255 value
val = constrain(val, 0, 255); //constrain it to 0~255 range
analogWrite(pwmpin, val); //voltage level input on motor
}
further exercise
[초급] 다른 센서(광센서, 압력센서 등)를 가지고 속도를 조절해 봅시다
[중.고급] 모터 속도 바꾸기를 활용해 재미있는 기구를 만들어 봅시다