Pages: [1]   Go Down
Author Topic: GSWA - Example 7B - p71 - Reading Fan Speed.  (Read 2349 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino Rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From Getting Started with Arduino, page 71, there is an image showing a method for turning on and off a motor. There is no arduino sketch included and no control switch, so I created a sketch using a 12V 40mm 3-pin computer fan, mosfet, zener diode, resistor, momentary switch and a 9V power source. I'm successful and turning the fan on and off, but am not sure what I'm seeing with regards to output from the yellow wire. When the fan is off, the reading from the analog 0 pin reads 1023. When the fan is on, I get a range of numbers between 0 and about 100, averaging somewhere around 9. These don't look like fan speed in RPM to me. (See example serial monitor listing, patch diagram and sketch below.) When I remove the patch wire between A0 and the yellow fan wire completely, I get analog numbers on the serial monitor averaging around 350, regardless of whether the fan is spinning or not. So I'm not sure what I'm seeing.

I suspect I need an actual sample method for A0, but am not sure where to begin, nor how to incorporate it with other commands that include delays within the loop. I would be grateful for some suggestions about how to measure the input from A0 so I can get a valid RPM on my fan.

Other thoughts:
I tried using pin ~10 in place of A0, but got the same numbers as A0 without patch cord.
Since the original exercise alluded to PWM for changing fan speed, I thought I'd try
    analogWrite(CONTROL, 128); // turn Fan ON
to get half speed, but ended up with a very high pitched whine emitting from the fan. It did spin at a visibly slower rate. I'm unsure as to whether continued use in this way could damage the motor or the board. I'd like to know one way or the other about this too.

Cheers

Code:
fan speed: 1023 <-- fan off
fan speed: 1023
fan speed: 1023
fan speed: 0    <-- fan on
fan speed: 20
fan speed: 19
fan speed: 10
fan speed: 77
fan speed: 0
fan speed: 8
fan speed: 9
fan speed: 32
fan speed: 8
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 13
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 11
fan speed: 16
fan speed: 19
fan speed: 0
fan speed: 8
fan speed: 15
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 14
fan speed: 0
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 7
fan speed: 12
fan speed: 9
fan speed: 8
fan speed: 15
fan speed: 0
fan speed: 9
fan speed: 15
fan speed: 0
fan speed: 24
fan speed: 115
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 0
fan speed: 7
fan speed: 24
fan speed: 0
fan speed: 9
fan speed: 9
fan speed: 0
fan speed: 7
fan speed: 8
fan speed: 14
fan speed: 0
fan speed: 7
fan speed: 7
fan speed: 15
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 7
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 12
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 7
fan speed: 0
fan speed: 7
fan speed: 8
fan speed: 16
fan speed: 7
fan speed: 52
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 10
fan speed: 15
fan speed: 0
fan speed: 0
fan speed: 10
fan speed: 13
fan speed: 11
fan speed: 19
fan speed: 0
fan speed: 96
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 20
fan speed: 79
fan speed: 25
fan speed: 9
fan speed: 20
fan speed: 8
fan speed: 14
fan speed: 0
fan speed: 8
fan speed: 8
fan speed: 1023 <-- fan off
fan speed: 1023
fan speed: 1023
fan speed: 1023
fan speed: 1023
fan speed: 665  <-- remove wire from A0
fan speed: 426  <-- ???
fan speed: 351  <-- ???
fan speed: 344
fan speed: 338
fan speed: 342
fan speed: 337
fan speed: 340
fan speed: 336
fan speed: 340
fan speed: 336

board patch diagram:


sketch:
Code:
// Example 07B: Detect Analog Sensor and send to Serial Port

#define SENSOR 0 // The pin for the Fan Sensor
#define CONTROL 6 // Control Pin
#define BUTTON 7 // Start and Stop Button

int motorspeed = 0; // Variable used to store the value coming from the sensor.
int val = 0; // State of Pin 6
int old_val = 0; // This variable stores the previous value of "val".
int state = 0;  // 0 = CONTROL off while 1 = CONTROL on
int delayTime = 5000; // Five seconds in milliseconds.
int thisTime = 0; // A counter to compare with delayTime.

void setup() {
  // pinMode(SENSOR, OUTPUT); // tried PWM pin 10, didn't work. Same as no A0 patch.
  pinMode(CONTROL, OUTPUT); // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input.
}

void loop(){
    val = digitalRead(BUTTON); //read input value and store it.
  
  // Check if there was a transition
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state; // turn LED ON.
    // delay(10); // debounce delay.
  }
  
  old_val = val; // val is now old. Let's store it.
  
  if (state == 1) {
    digitalWrite(CONTROL, HIGH); // turn Fan ON.
    // analogWrite(CONTROL, 128); // turn Fan ON.
    // Tried this but got a high-pitched whine from the fan. Probably not good.
  } else {
    digitalWrite(CONTROL, LOW); // turn Fan ON.
  }

  thisTime = thisTime + 100; // Increment current delayTime.
  motorspeed = analogRead(SENSOR); // Read the value from the sensor.
  if (thisTime > delayTime){
  // Write to serial port every time thisTime is greater than delayTime.
  Serial.begin(9600); // Open serial port to send data to computer at 9600 bps.
  Serial.print("fan speed: "); // Send this text.
  Serial.println(motorspeed); // Send the value and a line feed.
   thisTime = 0; // Reset our counter.
  }
  delay(100); // 1/10th second delay.
}
« Last Edit: January 10, 2011, 09:17:35 pm by greenman » Logged

Pages: [1]   Go Up
Jump to: