November 10, 2020

Pcomp4 - Motors

Through the motor, try to convert electrical energy into mechanical energy…


1.Hook up a servo

Hook up a servo and make it move using pwm.

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
// Chapter 5 - Motor Control
// Controlling Speed
// By Cornel Amariei for Packt Publishing

// Include the Servo library
#include <Servo.h>

// Declare the Servo pin
int servoPin = 3;
// Create a servo object
Servo Servo1;

void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}

void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 45 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(360);
delay(1000);
}



2.Hook up a motor

Motor can convert the signal into mechanical power, help us drive the external mechanical device.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop(){
switchState = digitalRead(switchPin);
if(switchState == HIGH) {
digitalWrite(motorPin, HIGH);
}
else {
digitalWrite(motorPin, LOW);
}
}



3.Hook up an MPR121

Create a circuit I control with a touch sensor.

The touch sensor is so sensitive that it detects different values, like operating a touch screen to output values.



About this Post

This post is written by Siqi Shu, licensed under CC BY-NC 4.0.