Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 2 | ||||
Software apps and online services | ||||||
| ||||||
Hand tools and fabrication machines | ||||||
|
I bought my first magnetic stirrer to help improve my mushroom liquid cultures. After seeing how much the constant stirring improved the culture, I decided I wanted more of them. After reading a few articles about the magnetic stirrers, I found that I could use an Arduino as the control signal (PWM) to drive PC fans and make a batch of magnetic stirrers for about the same price as one entry level stirrer.
/*
Magnetic Stirrer
Layman summary:
We will use a potiometer to control the speed of a DC motor.
When the potiometer is turned to full Ohms then the motor should stop.
When the potiometer is turned to 0 Ohms then the motor should be running full speed.
And if the potiometer is somewhere in the middle then the motor speed will follow.
InDepth summary;
This code will read an analog voltage of the potiometer using pin(A0).
We will take the analog to digital conversion (ADC) value and
produce a corresponding PWM signal on pin(9). The PWM signal will turn the
NPN transistor on/off very quickly to the point where the motor will spin like
it is getting less than full power.
Notes:
1. Please note the ADC on the Arduino mega is 10 bits. The PWM is 8 bits.
So we need to divide the ADC value from pin(A0) by 4 and then send that value to
the PWM on pin(9).
2. Intersting to note that if you leave out the division then
you will notice that a 1/4 turn of the potiometer will give you a 0 rpm to full speed
on your motor. This is because 8 bits will get you 255 steps for the PWM and the
ADC 10 bits will give you 1023 steps. 1023/255 = 4 (roughly)
3. I added a delay at end of loop to help stablize motor speed. I noticed
with PC fans I was using that if you rotated the potiometere quickly or back and fourth
that the load of the magnets and the drastic speed up/down would burn out the
cheap PC fan motors. So a word of caution is to turn the pot slowly to avoid motor burn out :)
Brief Function description:
analogWrite() allows us to send a 0 to 255 value to a PWM pin.
ReadAnalogVoltage() allows us to read a 0 to 5Vdc voltage and convert
voltage into a value between 0 to 1023.
Serial.Began starts serial communication to "Serial Monitor"
Serial.PrintLn writes one line to "Serial Monitor"
Credits:
Giving credit to the contributors of the example (basic) "Fade" program
and "ReadingAnalogVoltage" program since this code is really just a cut/paste of
both example programs into one working code.
leaving the original ReadAnalogVoltage notes since I found them useful as well :)
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
int motor1 = 9; // the PWM pin
int motor1speed = 0; // how fast motor1 is
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(motor1, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int motor1speed = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 12V):
float voltage = motor1speed * (12.0 / 1023.0);
// print out the value you read:
Serial.println(motor1speed);
// print out the voltage
Serial.println(voltage);
// set the motor1speed of pin 9:
analogWrite(motor1, motor1speed/4);
// delay 30 miliseconds. this pre
delay(30);
}
Comments