• No se han encontrado resultados

LISTA DE ANEXOS

5. MARCOS DE REFERENCIA 1 MARCO TEORICO

5.1.1 Los determinantes sociales (ver anexo A)

Now that we have a handle on PWM, I will look at other ways to utilize it. For this circuit, I will use a potentiometer connected to an analog input which will control the speed of the fan

connected to a PWM output. The goal is to adjust the speed of the fan based on the analog input read in from the potentiometer. For practical uses, I would most likely put a temperature sensor on the analog input in order to adjust the fan based on the current temperature. However for this example, I will simply utilize the potentiometer to better exercise the fan from off to full on.

Arduino® Board 10K potentiometer 2N2222(A) Transistor 100 Ohm Resistor 1K Ohm Resistor 150 Ohm Resistor Fan, +12v, 200mAmp TOOLS breadboard jumper wires voltage meter SCHEMATIC

The fan connects to the board using a four (4) pin header. A three (3) pin fan can also be used however it will not have the ability to be controlled via a PWM output pin unless you hook this value up to a transistor such as Q1 in the diagram below. The voltage for the fan should be applied to the resistor on the top of the board to flow into the transistor. This assumes of course that the fan you are using is designed to run off of +12v. If you are using the four (4) pin fan type then you will want to change the yellow wire from the transistor to connect to the +12v voltage input for the fan, bypassing the transistor all together. The main thing to note is to not connect the fan power to the Arduino® as it may overload the Arduino® circuit board and damage it. You will also need to consider the current draw, Ic, to ensure it doesn't overload the transistor. For the transistor specified, the maximum current, Ic, is 1.0 Amps.

Image created using:

SOFTWARE

The following code is used to read in the voltage across the potentiometer and in turn write out to the PWM pin in order to control the speed of the fan.

/*

* FanPWM - the purpose of this sketch is to read in a value from a potentiometer and then

* use that value to determine how fast the fan should be spinning. *

* The fan control pin is where the input from the potentiometer will be read from.

*

* fanTachPin is connected to read in the current speed of the fan *

* The fan speed pin is the where the PWM output of the Arduino will connect to

* the interface circuit in order to drive the fan. *

* The voltage reference is the max voltage that can be read in on the * analog input pin

*

* The max sample count is the highest value that could be read in on the * analog input pin which will equal the voltage reference

*/

const int fanControlPin = 0; const int fanTachPin = 7; const int fanSpeedPin = 9;

const float voltageReference = 5.0f; const int maxSampleCount = 1023;

/*

* Method declarations */

/* No additional methods in this sketch */ /*

* Setup - main configuration point of our sketch to configure * the Arduino for analog input and PWM output

* also set up the tach pin for input along with * enabling the serial port to monitor fan speed * * Params - none * * Returns - nothing */ void setup() { analogReference(DEFAULT); pinMode(fanTachPin, INPUT); Serial.begin(9600); } /*

* loop - main method which will be called each time the * Arduino goes through its main loop to read the * current fan control value and then turn on the * fan based on the voltage across the potentiometer * * Params - none * * Returns - nothing */ void loop() { int fanControlValue = 0; int fanSpeed = 0; int fanTach = 0;

// Read in the temperature value which will be in a range from 0 to 1023 fanControlValue = analogRead(fanControlPin);

// Our range for output for PWM is 0 to 255 so we will divide by // 4 in order to get the read in value into the proper range // the output pin expects.

fanSpeed = fanControlValue / 4;

analogWrite(fanSpeedPin, fanSpeed);

// Read in the fan tachometer value which will be a high signal // for a period X

fanTach = pulseIn(fanTachPin, HIGH);

char buffer[256];

int rpm = 1000000 / fanTach; // 1 Million as the value is in microseconds

// Target and tach shouldn't be expected to be the same sprintf(buffer, "Target: %d, Tach: %d", fanSpeed, rpm);

Serial.println(buffer);

// Rest for a second and check again delay(1000);

}

CONSTANTS

In the beginning of this sketch I have declared some constant values which I will use later on in my code. As I mentioned previously, it is better to declare these values as constant that way you get the benefit of meaningful names in your code which helps in the overall readability of your sketch along with the ability to change the value as needed later on without trying to find everywhere it is used in your code. The constants in this sketch define which pins are being used as analog and digital input, and which pin is being used for analog out i.e. PWM output.

METHOD DECLARATIONS

There are not any additional methods declared for this sketch. SETUP

The setup method is where I will configure my Arduino® to operate as I need. For this sketch we will ensure that our analog reference voltage is five (5) volts.

void setup() { analogReference(DEFAULT); pinMode(fanTachPin, INPUT); Serial.begin(9600); }

In addition to the analog reference we will also set the fanTachPin as a digital input which is much like we have done before however there will be a slight twist in how we use it. Finally the serial port is started and set for 9600 baud. This will allow us to observe the speed of the fan that is being read.

As before, the loop method is where all of the work is done. In this case we will be reading in a value from a potentiometer and writing out a value to the fan which represents how fast the fan should be spinning. In addition we will be reading in the pulse width of the fan tachometer pin and presenting that as a speed to the serial port interface.

void loop() { int fanControlValue = 0; int fanSpeed = 0; int fanTach = 0;

// Read in the temperature value which will be in a range from 0 to 1023 fanControlValue = analogRead(fanControlPin);

// Our range for output for PWM is 0 to 255 so we will divide by // 4 in order to get the read in value into the proper range // the output pin expects.

fanSpeed = fanControlValue / 4;

analogWrite(fanSpeedPin, fanSpeed);

// Read in the fan tachometer value which will be a high signal // for a period X

fanTach = pulseIn(fanTachPin, HIGH);

char buffer[256];

int rpm = 1000000 / fanTach; // 1 Million as the value is in microseconds

// Target and tach shouldn't be expected to be the same sprintf(buffer, "Target: %d, Tach: %d", fanSpeed, rpm);

Serial.println(buffer);

// Rest for a second and check again delay(1000);

}

The voltage is read in from the fan control pin and converted to a value that is proper for our

PWM output. The input range is from 0 to 1023 while the output can only be from 0 to 255. Because

of this, I will scale the incoming value by dividing the value read by four (4). Once we have

converted our value, we will then write that speed out to set the fan for that speed. Note that the actual value written will not be an actual speed but a value from 0 to 255. The fan most likely will turn off well before we reach the lowest value i.e. 0 and will turn on when not zero (0). The reason for this is that it takes a bit of voltage in order to actually switch on the transistor and depending on what

components that you use along with which fan and what voltage you select. Once the value has been written to the fan speed pin, I will read in the value from the tachometer in order to determine the speed that the fan is rotating.

// Read in the fan tachometer value which will be a high signal // for a period X

fanTach = pulseIn(fanTachPin, HIGH);

For this sketch, I have included a new input method for digital pins. This is the pulseIn method which is designed to read in the length of either a HIGH or LOW signal. This method will return a value representing the amount of time a pulse was either HIGH or LOW. This value will represent the number of microseconds that the pulse endured. In the above example, I am measuring how long the pulse is HIGH in order to determine the rotational speed of the fan. The return value from the pulseIn method is in microseconds. So our rotations will be one divided by the number of microseconds. I was reading around 500 microseconds on my display which puts us at an RPM of around 2000 which is inline with what I would expect based on the documentation for the fan.

Once we know what the rotational speed is of the fan, we will display the target speed and tachometer value to the user. Following this we will sleep for one (1) second before checking the potentiometer again and adjusting the fan speed accordingly. You might notice that the fan tach is not stable and can rise and fall quite significantly. We will rectify this in the chapter regarding .

This type of circuit would be similar to what your mother board in your computer might do in terms of controlling the CPU temperature. Basically the system will want to measure the current

temperature of the CPU and increase the fan speed if it is getting above a certain point and decrease it when it drops back down to a certain threshold. If we were to take our circuit from the chapter on

and add the fan interface to that circuit, we could turn the fan on whenever the temperature reached our set point.