Lab 1 - Controlling a dc motor with a potentiometer
const int transistorPin = 9; // connected to the base of the transistor
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
// read the potentiometer:
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
}const int transistorPin = 9; // connected to the base of the transistor
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
// read the potentiometer:
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
}
Lab 2 - Controlling a dc motor using an H-Bridge
const int switchPin = 2; // switch input
const int motor1Pin = 3; // Motor driver leg 1 (pin 3, AIN1)
const int motor2Pin = 4; // Motor driver leg 2 (pin 4, AIN2)
const int pwmPin = 9; // Motor driver PWM pin
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT_PULLUP);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(pwmPin, OUTPUT);
// set PWM enable pin high so that motor can turn on:
digitalWrite(pwmPin, HIGH);
}
void loop() {
// if the switch is high, motor will turn on one direction:
Serial.println(digitalRead(switchPin));
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the motor driver low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the motor driver high
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the motor driver high
digitalWrite(motor2Pin, LOW); // set leg 2 of the motor driver low
}
}
Lab 3 - Controlling a stepper motor using an H-Bridge
const int transistorPin = 9; // connected to the base of the transistor
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
// read the potentiometer:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
analogWrite(transistorPin, outputValue);
}