Author: Vio Zhu (yz9617) | Oct 9, 2023 | Fall 2023 section: Rios, Tuesday
const int transistorPin = 9; // connected to the base of the transistor
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
digitalWrite(transistorPin, HIGH);
delay(1000);
digitalWrite(transistorPin, LOW);
delay(1000);
}
Somehow the Potentiometer value read from microcontroller is always super small (like <50), which makes DC motor not moving. Turns out I misconnect the 1st pin of the 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 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 = 5; // Motor driver PWM pin
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// 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:
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
}
}
I can stop the motor by setting both the motorPin to LOW.
https://itp.nyu.edu/physcomp/lessons/dc-motors-the-basics/
inductive loads ( motors, electromagnets, and solenoids) work on this same principle: induce a magnetic field by putting current through a wire, use it to attract or repulse a magnetic body.