Sunday, 25 October 2020

Motors with Arduino

 For the Prototyping module for my Masters Engineering course, I've been given an Arduino Starter kit to play with.

There is, of course, a hidden reason for this - ultimately we have to prototype a robot arm and create a portfolio of the design work. But right now I'm working through the example projects from the Arduino Projects Book that comes in the kit.

I'm familiar with Arduino, and have used it for multiple projects in the past. But although I have used Servos, I have never actually used a DC motor with the Arduino Today is the day to rectify that.

Here's a Simulation made in Tinkercad.

On the left is a push switch connected to Pin2 with a 10k pull down resistor. The pull down resister makes Pin2 to be LOW - 0volts, unless the button is pressed, when Pin2 goes HIGH - 5volts.

This is detected in the code; if the switchState on Pin2 is HIGH, then the output on Pin 9 also goes HIGH.

Pin 9 is connected to the Gate of the transistor on the left. The middle pin is the Drain and the third pin is the Source. The Source pin is connected to Ground, while the Drain is connected to the negative lead of the motor. When a voltage is supplied by the Arduino tot he Gate, the Drain is connected tot he Source, and the negative lead of the motor is effectively connected to Ground, allowing the motor to turn. The motor has it's own 9V power supply, separate from the Arduino, so as not to overload the microcontroller.

There's also a clamping diode across the terminals of the motor, which means that when the motor is turned off, the back voltage that the motor creates white it continues to turn does not fry the circuit.


Here's the Arduino Code for this circuit.

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode (motorPin, OUTPUT);
  pinMode (switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  switchState = digitalRead(switchPin);

  if (switchState == HIGH) {
    digitalWrite(motorPin, HIGH);
  }
  else {
    digitalWrite(motorPin,LOW);
  }
}


Finally, here's the circuit I built on the breadboard.


CH32V203 Big Round Button

Having successfully uploaded the Blink code to the  CH32V203 , the next task is to try the HID button demo. The circuit I’m using has two bu...