Education

Education

Motor


What are Motors

A DC motor is like a little machine that makes things spin when you give it electricity. Imagine it as a tiny fan inside a toy car: when you connect it to a battery, the motor turns and makes the wheels move. This kind of motor is called "DC" because it runs on Direct Current, which is the type of electricity you get from batteries. DC motors are used in lots of fun projects, like robots, toy cars, and even in household appliances like fans!

Motor applications in real world

Motors are like the engines that make many everyday things move and work. Here are some fun examples of how motors are used in the real world:

  1. Toy Cars and Robots:
    Tiny motors help toy cars zoom around and robots move their arms or wheels.

  2. Fans:
    Motors spin the blades in fans to keep you cool on a hot day.

  3. Electric Vehicles:
    Cars, buses, and bikes can use electric motors to drive without gasoline.

  4. Household Appliances:
    Many kitchen gadgets like blenders, washing machines, and vacuum cleaners have motors to help them do their jobs.

  5. Power Tools:
    Electric drills, saws, and other tools use motors to make work easier.

  6. Drones:
    Small motors help drones fly high in the sky for fun and photography.

  7. Computers and Electronics:
    Some parts of computers, like cooling fans or hard drives, use motors.

  8. Helicopters and Airplanes:
    Motors (or engines) help these vehicles move through the air.

  9. Conveyor Belts in Factories:
    Motors move products along belts so they can be packaged or assembled.

  10. Household Curtains and Blinds:
    Motors can automatically open or close curtains, making life more convenient.

In all these examples, motors turn electrical energy into movement, helping make our lives easier and a lot more fun!How to connect Motor to Yellow tyro board

Existing Motor Code

// Check if the motor switch is pressed (reads HIGH because of the setup)
if (digitalRead(motorSwitchPin) == HIGH) {
  if (motorState == 0) {
    // Run motor in forward direction:
    // Turn on the forward motor pin and ensure the reverse pin is off.
    digitalWrite(motorForwardPin, HIGH);
    digitalWrite(motorReversePin, LOW);
    delay(2000);         // Run the motor forward for 2 seconds.
    motorState = 1;      // Change the state to indicate next press should run in reverse.
  } else {
    // Run motor in reverse direction:
    // Turn on the reverse motor pin and ensure the forward pin is off.
    digitalWrite(motorForwardPin, LOW);
    digitalWrite(motorReversePin, HIGH);
    delay(2000);         // Run the motor in reverse for 2 seconds.
    motorState = 0;      // Change the state to indicate next press should run forward.
  }
  // After running, turn off both motor control pins.
  digitalWrite(motorForwardPin, LOW);
  digitalWrite(motorReversePin, LOW);
  delay(300);            // Wait a little for the switch to be ready for another press.
}

Explanation

  • What This Code Does:
    When you press the button connected to the motor (the "motor switch"), the Arduino checks which way the motor should spin. It uses a memory spot called motorState to decide:

    • If motorState is 0:
      The motor spins forward. This means the code turns on the "forward" control (turning a specific pin on) for 2 seconds.

    • If motorState is 1:
      The motor spins in reverse. The code turns on the "reverse" control for 2 seconds instead.

  • How It Works:

    • The code reads the button. When the button is pressed, it looks at motorState:

      • If it's 0, the motor goes forward for 2 seconds, then motorState changes to 1.

      • If it's 1, the motor goes in reverse for 2 seconds, then motorState changes back to 0.

    • After running the motor, the code turns both controls off and waits a short time before checking the button again.

  • Why It's Cool:
    This snippet lets you change the motor's direction every time you press the button, so your motor can spin one way and then switch the other way on the next press. It's like having a toy car that goes forward, then reverses when you tap it!

Fun Activity!

Controlling the Motor Speed

Welcome to the Motor Speed Challenge using your Yellow Tyro Board from JumpLabs®! In this activity, you'll explore how you can control a motor's speed by turning numbers into motion. Here's what you'll do:

  1. Set Your Speed:
    Open the Serial Monitor on your computer and enter a number between 0 and 255. This number represents the speed at which your motor will run—the higher the number, the faster it goes!

  2. Press the Button:
    When you press the button connected to A0 on your Yellow Tyro Board, your motor (wired to pins D9 and D10) will spring into action and run forward at the speed you set.

  3. Watch It Go:
    See how changing the speed value affects the motor's performance. It's like having a remote control for a mini car, where you decide exactly how fast it moves!

/*
                         JumpLabs®
      Follow us on Instagram: jumplabs.co
                 LinkedIn: JumpLabs
         "Bringing Families Closer!"

 This sketch lets you enter a speed value (0-255) through the Serial Monitor.
 When you press the button (connected to A0), the motor runs at that speed.
 The motor is connected to D9 (for speed control) and D10 (for direction).
*/

const int motorForwardPin = 9;  // PWM pin to control motor speed (forward direction)
const int motorReversePin = 10; // Pin for reverse direction (set LOW for forward)
const int buttonPin = A0;       // Button connected to analog pin A0 (assumes external pull-down)

int speedValue = 0;             // Variable to store the user-entered speed (0-255)

void setup() {
  Serial.begin(9600);           // Start serial communication at 9600 baud
  Serial.println("Enter motor speed (0-255) and press the button to run the motor.");
  
  pinMode(motorForwardPin, OUTPUT);  // Set motor forward pin as output
  pinMode(motorReversePin, OUTPUT);  // Set motor reverse pin as output
  pinMode(buttonPin, INPUT);         // Set button pin as input (assumes external pull-down resistor)
  
  // Ensure the motor is off initially
  analogWrite(motorForwardPin, 0);
  digitalWrite(motorReversePin, LOW);
}

void loop() {
  // Read the speed value from the Serial Monitor if available
  if (Serial.available() > 0) {
    speedValue = Serial.parseInt();   // Read the entered speed value (0 to 255)
    Serial.print("Speed set to: ");
    Serial.println(speedValue);
  }
  
  // Check if the button is pressed (reads HIGH when pressed)
  if (digitalRead(buttonPin) == HIGH) {
    // Run the motor in forward direction at the given speed
    analogWrite(motorForwardPin, speedValue);  // Apply PWM signal to set motor speed
    digitalWrite(motorReversePin, LOW);          // Ensure reverse is off
  } else {
    // If the button is not pressed, stop the motor
    analogWrite(motorForwardPin, 0);  // Set speed to zero (motor off)
    digitalWrite(motorReversePin, LOW);  // Keep reverse off
  }
}

Code Explanation

  • Header Comment:

  • The purpose of the sketch: you enter a speed value (from 0 to 255) in the Serial Monitor, and when you press the button (on A0), the motor runs forward at that speed.

  • Pin Definitions:

    • motorForwardPin (D9) is used for PWM speed control.

    • motorReversePin (D10) is set LOW to ensure the motor runs in the forward direction.

    • buttonPin (A0) is used to trigger the motor when pressed (assumed to use an external pull-down resistor so it reads LOW when not pressed and HIGH when pressed).

  • Setup:
    In setup(), the Serial Monitor is started so you can input the motor speed. The motor pins and button pin are configured as outputs or inputs accordingly. The motor is initialized to the off state.

  • Loop:
    In loop(), the program:

    1. Checks if a speed value is entered via Serial and updates speedValue.

    2. Checks the state of the button:

      • If the button is pressed (reads HIGH), it sends a PWM signal on D9 using analogWrite() with the given speed value, turning on the motor.

      • If the button is not pressed, the motor remains off.

This sketch allows you to experiment with motor speed control interactively—enter a speed value, press the button, and see your motor run at that speed. Enjoy exploring motor control with your Yellow Tyro Board!

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us