[Fundamentals] Arduino - Making a Motor Control Fan

주피터 · 2019-10-25 · 6
Making a Natural Wind Fan ⓞ Appearance and internal structure image of a DC motor To rotate fan blades, it is good to use a DC motor. A DC motor has a coil wound on the rotor, and when current is applied, a magnetic field is generated. The rotor performs rotational motion through interaction with permanent magnets surrounding the coil. When the rotor rotates halfway, the direction of the magnetic field generated by the rotor reverses, so the polarity of the DC power supply must be reversed. This is accomplished by using brushes to supply opposite polarity DC power. DC motors can easily adjust rotation speed according to the supplied voltage and have strong rotational force, so they are used for various purposes. The PPN13KB09A DC motor is a small DC motor that can be rotated with a small voltage of 5V. It must have a current of at least 25mA to 100mA flowing through it to operate. However, the output terminal of the Arduino can only supply a maximum of 40mA. If a circuit is connected to allow more current to flow, the internal components of the Arduino may be damaged. ⓞ Transistor appearance, symbol, and internal structure explanation diagram Between the Arduino's output terminal and the motor, there must be an electronic component that acts like a lever. The Arduino's output terminal allows only a small current to flow, while the motor receives a separate large current. The large current flowing through the motor changes in proportion to the current change from the Arduino's output terminal. This lever-like component is a transistor. A transistor is similar to a diode, which has two legs, but it has one more leg, making it have three legs. The three legs are called the emitter, base, and collector, respectively. The P2N2222AG transistor we use is a transistor for general-purpose use, and it has the property of maintaining a ratio of approximately 100 times between the current flowing from the base to the emitter and the current flowing from the collector to the emitter. When 1mA of current flows into the base and 100mA flows into the collector, 101mA of current (the sum of the two currents) flows out from the emitter. Transistors can be used for two main purposes. The first is the switch function. When the current flowing from the base to the emitter is cut off, the current flowing from the collector to the emitter also stops flowing. When current flows from the base to the emitter, current also begins to flow from the collector to the emitter. The second is the amplification function. When the small current flowing from the base to the emitter changes, the large current flowing from the collector to the emitter also changes at the same ratio. For example, if the amplification factor is 100 times and the current flowing from the base to the emitter increases from 1mA to 2mA, the current flowing from the collector to the emitter increases from 100mA to 200mA. ⓞ Transistor specifications The motor is connected to the collector, and the maximum voltage between collector and emitter of the P2N2222AG transistor is 40V. The maximum current that can flow through the collector is 600mA, so a DC motor can be safely connected to the collector. The Arduino's output terminal is connected to the base, and the Arduino's output terminal outputs a maximum of 5 volts. The maximum voltage between the base and emitter of the transistor is 6V, so it is safe. ⓞ Pulse Width Modulation (PWM) To control the speed of a DC motor, the voltage output from the Arduino's output terminal must be something other than 0 volts or 5 volts, but somewhere in between. The Arduino's output terminal has no method to output any arbitrary voltage other than 0V and 5V. However, using a method called "pulse width modulation" can produce a similar effect. The pulse width modulation technique rapidly and repeatedly outputs 0V and 5V to adjust the total amount of current delivered to the component connected to the output terminal, achieving the same effect as outputting an intermediate voltage. For example, if 2.5V output is needed, 0V and 5V are alternately output at equal time intervals. Then, when viewed over a larger time interval, the same amount of current flows out as if 2.5V were continuously supplied, so the same effect is obtained. If output of 3.75V, which is 75% of 5V, is needed, rapidly repeat the process of outputting 5V three times and then 0V once. ⓞ Arduino PWM output terminal Not all digital input/output terminals of the Arduino can use the PWM technique. The Arduino board has 14 digital input/output terminals, and only 6 terminals marked PWM (3, 5, 6, 9, 10, 11) support PWM output. To perform analog output in a sketch, call the analogWrite(output terminal number, value) function. The value can be set from 0 to 255, and depending on the value, 0V to 5V is output. ⓞ random() function Calling the random() function generates an arbitrary random value. If you provide a maximum value in the parentheses, it generates a random integer greater than or equal to 0 and less than the maximum value. If you provide minimum and maximum values in the parentheses, it generates a random integer greater than or equal to the minimum value and less than the maximum value. However, there is a slight drawback. Although the random() function generates a random value each time it is called, it is not 100% completely random. When the sketch program restarts, that is, when the Arduino board is powered on again, random values are generated in the same order as before. Therefore, it is necessary to set the starting reference point of the random value differently each time. This starting reference point can be specified by providing a seed value as an argument to the randomSeed() function. To provide a different seed value each time, use the analogRead(terminal number) function. An input terminal not connected to anything is in an unstable state, so reading it with analogRead() gives an arbitrary value between 0 and 255 each time. This value is used as the seed value. ⓞ Circuit connecting resistor, transistor, and motor to Arduino Connect a resistor to terminal 9 of the Arduino and connect the base of the transistor to it. The resistor limits the current from the Arduino's output terminal to within 40mA and also limits the current flowing into the transistor. The emitter of the transistor is connected to ground (0V) and the DC motor is connected to the collector. The other side of the DC motor is connected to the 5V power supply. One of the six terminals supporting PWM must be used. ⓞ Component list Let me organize what components need to be prepared: First, 1 transistor, 1 DC motor and 1 propeller each, 1 breadboard to insert components, 1 set of jumper wires to connect components, 1 Arduino board, 1 USB cable to connect the Arduino board and computer, and 1 personal computer set are needed. ⓞ Circuit configuration and circuit diagram Let us place components on the breadboard according to the circuit configuration and connect them using jumper wires. After everything is connected, verify the direction of components and leg order once more. ⓞ Circuit configuration and circuit diagram A 10 kilohm resistor was used for resistor R1. The Arduino output terminal can output a maximum of 5V. According to Ohm's law, 5 volts divided by 10 kilohms gives a maximum current of 0.5mA flowing through the transistor's base. Since 0.5mA is much smaller than the Arduino's maximum allowable current of 40mA, there is no risk of damaging the Arduino. Also, assuming a transistor amplification factor of 100, the motor will have a maximum current of 0.5 milliamps times 100, which is 50mA, making the motor safe as well. ⓞ Explanation of the role of flyback diode Looking next to the motor in the circuit diagram, you can see a diode being used that was not in the plan. This diode is called a flyback diode. Inside the DC motor is a coil that acts as an electromagnet, and each time this coil rotates halfway, the brush contact is reversed, reversing the magnetic field of the coil. However, at the moment the brush contact is reversed, that is, the instant the coil's power supply is cut off, the magnetic field that was formed in the coil resists the sudden drop in voltage and generates a large back voltage. This back voltage causes sparks to jump at the brush contact points, which can damage the motor or cause a fire. Therefore, the flyback diode allows the current caused by this back voltage to bypass and flow, protecting the motor. ⓞ Sketch flowchart In the initial setup() function, one output terminal is set to output mode. In the loop() function, first the motor is turned on. Then wait 3 seconds, turn off the motor, and wait another 3 seconds. This process repeats infinitely. ⓞ Written sketch First, to use terminal 9, an integer variable motorPin was declared and assigned the value 9. In the setup() function, the pinMode function was called to set the motorPin terminal as output. Next, in the loop() function, the motorOnThenOff() function was called. The motorOnThenOff() function is not a built-in function in the sketch; it is a user function. That is, we must write the function ourselves. In the motorOnThenOff() function, call the digitalWrite() function to turn the motor on, call the delay() function to wait 3 seconds, then call the digitalWrite() function again to turn the motor off, and call the delay() function to wait 3 seconds. Upload the written sketch to the Arduino and verify that the motor repeats the operation of rotating for 3 seconds then being off for 3 seconds. ⓞ Sketch flowchart In the initial setup() function, one output terminal is set to output mode. In the loop() function, first the motor is rotated rapidly (200). Then wait 3 seconds, rotate the motor slowly (50). Wait another 3 seconds. This process repeats infinitely. ⓞ Written sketch screenshot In the loop() function, the motorOnThenOff() function was commented out, and the motorOnThenOffWithSpeed() function was newly called. In the motorOnThenOffWithSpeed() function, the analogWrite() function is called to rotate the motor fast, the delay() function is called to wait 3 seconds, then the analogWrite() function is called again to rotate the motor slowly, and the delay() function is called to wait 3 seconds. Upload the written sketch to the Arduino and verify that the motor repeats the operation of rotating fast for 3 seconds then slowly for 3 seconds. ⓞ Sketch flowchart In the initial setup() function, one output terminal is set to output mode. In the loop() function: In the first loop, i starts at 0 and increments by 1 to 255, rotating the motor at speed i and waiting 25 milliseconds repeatedly. In the second loop, i starts at 255 and decrements by 1 to 0, rotating the motor at speed i and waiting 25 milliseconds repeatedly. Wait 5 seconds. This process repeats infinitely. ⓞ Written sketch The setup() function has no modifications. Next, in the loop() function, the motorOnThenOffWithSpeed() function call was commented out and motorAceleration() function was called. The motorAcceleration() function is added below the loop() function. In the first loop, i starts at 0 and increments by 1 to 255, the analogWrite() function is called with i as the value, and 25 milliseconds is waited repeatedly. In the second loop, i starts at 255 and decrements by 1 to 0, the analogWrite() function is called with i as the value, and 25 milliseconds is waited repeatedly. After that, wait 5 seconds. This process repeats infinitely. ⓞ Sketch flowchart In the setup() function, set the output mode and specify a random seed value. Also declare a prevEnd variable and set its initial value to 127, the midpoint. In the loop() function, generate a random value, assign it to the newSpeed variable, and vary the motor speed from prevEnd to newSpeed. Finally, assign the newSpeed value to the prevEnd variable and return to the beginning. ⓞ Sketch screenshot 1 Declare an integer variable prevEnd to store the previous motor speed. Next, in the setup() function, call the randomSeed() function and specify the seed value as the value read by analogRead() function from terminal 0, which is not connected to anything. For debugging, also set the serial port speed to 9600 baud. Next, in the loop() function, first comment out the motorAcceleration() function call and call the naturalWind() function with prevEnd value as an argument. The returned result is assigned back to the prevEnd variable. ⓞ Sketch screenshot 2 The naturalWind function receives the prevEnd value and must return an integer value. First, declare an integer variable newSpeed and generate a random value to assign it. For debugging, print the prevEnd and newSpeed values to the serial port. ⓞ Sketch screenshot 3 If the newSpeed value is greater than prevEnd, repeat the loop starting from prevEnd and incrementing by 1 until it reaches newSpeed. Inside the loop, call the analogWrite() function to output the i value and delay for 25 milliseconds. If the newSpeed value is less than prevEnd, simply reverse the start and end values. Finally, return the value of the newSpeed variable. ⓞ Completed photo Now you can see the motor's speed continuously changing irregularly. In the serial monitor, you can see the start and end speeds. ⓞ Error cases When the motor does not run, first verify that the transistor terminal order is properly connected. The P2N2222Ag we use has different terminal arrangement than most other transistors, so it is easy to confuse. When the motor runs too weakly, check the model name and operating voltage/current of the motor used. Also verify that the resistor connected to the Arduino's output terminal is 10 kilohms. When the motor speed does not change, verify that the motor is properly connected between the transistor's collector and 5V, and that Arduino terminal 9 is connected to the transistor base through a 10 kilohm resistor. Sometimes the sketch program is not loaded onto the Arduino board. In most such cases, it is because the serial port number is incorrect. Try changing the port number in the Arduino development environment menu > Tools > Serial Port. We created a fan using a DC motor and a single-board computer that mimics natural wind. DC motors are used in many places besides fans. Examples include electric scooters used for food delivery or convenient transportation, subways that many people use conveniently, camera lenses that automatically adjust focus when taking photos, robot arms that launch satellites and assemble objects in industrial settings, vacuum cleaners commonly used at home, washing machines, and countless other applications. ``` int motorPin = 9; int prevEnd = 127; void setup(void) { pinMode(motorPin, OUTPUT); randomSeed(analogRead(0)); Serial.begin(9600); } void loop(void) { //motorOnThenOff(); //motorOnThenOffWithSpeed(); //motorAcceleration(); prevEnd = naturalWind(prevEnd); } void motorOnThenOff() { digitalWrite(motorPin, HIGH); delay(3000); digitalWrite(motorPin, LOW); delay(3000); } void motorOnThenOffWithSpeed() { analogWrite(motorPin, 200); delay(3000); analogWrite(motorPin, 50); delay(3000); } void motorAcceleration() { for(int i = 0; i <= 255; i++) { analogWrite(motorPin, i); delay(25); } for(int i = 255; i >= 0; i--) { analogWrite(motorPin, i); delay(25); } delay(5000); } int naturalWind(int prevEnd) { int newSpeed = random(0,256); Serial.print("Start: "); Serial.println(prevEnd); Serial.print("End: "); Serial.println(newSpeed); if( newSpeed > prevEnd ) { for(int i = prevEnd; i <= newSpeed; i++) { analogWrite(motorPin, i); delay(25); } }else{ for(int i = prevEnd; i >= newSpeed; i--) { analogWrite(motorPin, i); delay(25); } } return newSpeed; } ```
6
댓글 0
등록된 댓글이 없습니다.