.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. šŸ‘‰ Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _ar_motor: 2.13 Motor ========== Overview -------- In this lesson, you will learn how to use Motor, the working principle of which is that the energized coil is forced to rotate in the magnetic field then the rotor of the motor rotates accordingly on which the pinion gear drives the engine flywheel to rotate. Components Required ------------------- .. image:: img/list_2.13.png * :ref:`cpn_mega2560` * :ref:`cpn_breadboard` * :ref:`cpn_wires` * :ref:`cpn_l293d` * :ref:`cpn_motor` * :ref:`cpn_power_module` Fritzing Circuit ---------------- In this example, we use Power Supply Module to power the anode and cathode of breadboard. GND of Mega 2560 Board is connected to the cathode. .. image:: img/image141.png Schematic Diagram ----------------- .. image:: img/image459.png Code ---- .. note:: * You can open the file ``2.13_motor.ino`` under the path of ``sunfounder_vincent_kit_for_arduino\code\2.13_motor`` directly. * Or copy this code into Arduino IDE. .. raw:: html After uploading the codes to the Mega2560 board, you can select the rotating direction of motor by typing 怌A怍 or 怌B怍 in the serial monitor. Code Analysis ------------- The motor can be driven by providing a voltage difference between the copper sheets at both sides of the motor. Therefore, you only need to write 0 for the voltage of one side of the copper sheet and 5V for the other side. Modify the written analog signal value to adjust the direction and speed. .. code-block:: arduino void clockwise(int Speed) { analogWrite(motor1A,0); analogWrite(motor2A,Speed); } void anticlockwise(int Speed) { analogWrite(motor1A,Speed); analogWrite(motor2A,0); } In this example, Serial.Read() is used to control the direction of motor. When you type \'A\' in serial monitor, there calls the clockwise (255) function to make the motor rotate with the speed of 255. Input \'B\', and the motor will rotate in reverse direction. .. code-block:: arduino void loop() { if (Serial.available() > 0) { int incomingByte = Serial.read(); switch(incomingByte){ case 'A': clockwise(255); Serial.println("The motor rotate clockwise."); break; case 'B': anticlockwise(255); Serial.println("The motor rotate anticlockwise."); break; } } delay(3000); stopMotor(); } Phenomenon Picture ------------------ .. image:: img/image143.jpeg