3. Zoetrope

Introduction

In this project, we aim to create a device called a Zootropio, capable of giving the illusion of movement through a sequence of images. To make the project more engaging, we will use a motor, a switch to control the direction, another switch to turn it on and off, and a potentiometer for speed control.
To enable the carousel to rotate in one direction only, we will introduce H-Bridges.

Bridge H

H-Bridges are known as integrated circuits (ICs). Integrated circuits are components that contain complex circuits within a very small space. You can access the integrated circuit through the pins that extend from the sides.

Necessary components

Construction

  1. Connect the power and ground to one side of the breadboard.
  2. Add 2 push buttons to the breadboard, connecting each one to a 10 kilo-ohm resistor with the other end of the resistor connected to ground. The button on pin 4 controls the direction, while the other button turns the motor on and off.
  3. Connect the potentiometer to the breadboard. Connect 5 volts to one side and ground to the other side. Finally, connect the center pin to Arduino's analog input 0.
  4. Place the H-Bridge on the breadboard. Connect pin 1 of the H-Bridge to Arduino's digital pin 9. This pin controls the motor; applying 5 volts turns it on, while 0 volts turns it off, enabling pulse-width modulation for speed control.
  5. Connect pin 2 of the H-Bridge to Arduino's digital pin 3, and pin 7 to digital pin 2. These pins communicate with the H-Bridge to specify the direction of rotation.
  6. The H-Bridge is powered from pin 16; connect it to 5 volts, and pins 4 and 5 should be connected to ground.
  7. Connect the motor to pins 3 and 6 of the H-Bridge.
  8. Connect pin 8 of the H-Bridge to the positive terminal of the battery.

IMG_4864.jpg

Code explanation

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOffSwitchStateSwitchPin = 5;
const int potPin = A0;

int onOffSwitchState = 0;
int previousOnOffSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;

int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;

void setup() {

  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  
}

void loop() {

  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);
  directionSwitchState = digitalRead(directionSwitchPin);
  motorSpeed = analogRead(potPin)/4;

  if(onOffSwitchState != previousOnOffSwitchState){
    if(onOffSwitchState == HIGH){
      motorEnabled = !motorEnabled;
    }
  }

  if(directionSwitchState != previousDirectionSwitchState){
    if(directionSwitchState == HIGH){
      motorDirection = !motorDirection;
    }
  }

  if(motorDirection == 1){
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
  }else{
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
  }

  if(motorEnabled == 1){
    analogWrite(enablePin, motorSpeed);
  }else{
    analogWrite(enablePin, 0);
  }

  previousDirectionSwitchState = directionSwitchState;
  previousOnOffSwitchState = onOffSwitchState;

}

Declared Variables:

setup() Function:

loop() Function:

In summary, this code allows control of a motor using Arduino, enabling the user to adjust motor direction and speed using switches and a potentiometer.