Thank you for talking the time to read this. I am new to the world of Arduino projects and I'm trying to automate my window mounted A/C unit. I would like the controller to rotated a servo 35 degrees when the temp. senor output is higher then the temp that I set with a potentiometer(within a set range) and vice versa. I would like all of the data to be outputted to a external screen that I have as well.
The extent of my coding experience has only been with matlab and a little of python so this is all new territory for me. So far I've manage to get the temp sensor working but that's it and I have no ideal how to even begin processing the temp data and to move a servo. If you guys could point me to some resources, I would greatly appreciate it.
//TMP36 Pin Variables
int sensorPin = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage
float voltage = reading * 5;
voltage /= 1024.0;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// calculations of temp C, print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC); Serial.println(" degrees C");
// now convert to F
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
delay(1000);
Have a look at the examples that come with the servo.h library.
I would like the controller to rotated a servo 35 degrees when the temp. senor output is higher then the temp that I set with a potentiometer(within a set range) and vice versa. I would like all of the data to be outputted to a external screen that I have as well.
Getting a servo to turn a knob is a little questionable, i would rather control the AC directly, but there is advantages to your approach. You should really tell us what kind of screen.
Hi ailun_y,
welcome to the Arduino-Forum.
first I want to ask what your code shall do in the end.
Moving the servo 35degrees is not a selfpurpose. there is something "behind" it.
Please asnwer this question. In a lot of cases better working solutions can be found if the real purpose is known.
My guessing is: switching On/Off your automatic climatisation-unit depending on a measured temperature. Is this right?
Anyway there is a library called ControlledServo which offers additional functionality over the standard-servo-library
Are you already familiar with how to install additional libraries into arduino-IDE?
The temperature-sensor you are using is an anlog one.
There is some potential of wrong measurings with using analog-sensors through electromagnetic noise. To get this avoided you need quite some knowledge about analog electronics.
This sensor TMP36 has an output 0,01V/°C
which is in the range of 0,5V (at 0°C) up to 1V (at 50°C)
What resolution does your ADC have? from your calculations * 5 / 1024 it seems 8 bit = 256 steps
with a reference-voltage of 5V this means resolution is
5000 mV / 256 = 19 mV per digit the acuracy will be 2 °C
I would recommend using a onewire-temperature-senssor called DS18B20. This sensors works "digital" which means the signal from the sensor is transferred digital to the arduino. This digital transmission is much more robust against electromagnetic noise than analog sensors are. This needs another library called onewire but it is quite easy to use. The resolution is 0,0625 °C the absolute accuracy is 0.5°C.
So please answer the above questions to get more adequate answers
best regards
Stefan
What resolution does your ADC have? from your calculations * 5 / 1024 it seems 8 bit = 256 steps
i think he is calculating the voltage (5v ref) and then dividing by the ADC resolution (1024 or 1023) that would be 10-bit (as Arduino usually have)
OK this results in a resolution of 5000/1024 = 5mV = 0,5°C.
stays the sensivity to electromagnetic noise
best regards
Stefan
Now, you problem is to interface the Servo (Fig-1, SG90) with the temperature sensor -- correct? Follow these steps:

Figure-1:
1. Making the Servo working alone.
(1) Locate the 3 wires of the Servo from Fig-2 and connect them with UNO in this way--
5V of UNO ----> Red ( Vcc) wire of Servo
GND of UNO --> Brown (Ground) wire of Servo
DPin-3 of UNO ---> Orange (PWM) wire of Servo

Figure-2:
(2) Upload the following sketch and check that the servo's shaft has rotated to about 450 position.
#include <Servo.h>
Servo myServo;
void setup()
{
myServo.attach(3);
Serial.begin(9600);
myServo.write(0); //zero position
delay(1000);
myServo.write(45); //45 degree rotation
}
void loop()
{
}
2. Is Step-2 working? Now, think about passing the temperature value as an argument of myServo.write(arg) method. Are you familiar with map() function which you can use to compress the 'int type' temperature value.


Deva_Rishi:
Getting a servo to turn a knob is a little questionable, i would rather control the AC directly, but there is advantages to your approach. You should really tell us what kind of screen
I'm using a LCD1602 with a I2C adapter. I've manage to display the output of the readings to it and now I'm working on adding the functionality of the potentiometer. I was initially going to use that method but I just got a 3D printer and with this extra time I thought I'd get my hands dirty and learn the in and out of that by modeling a housing for the servo and clamps for the "knobs".
OK so you seem to be on rails realising this project servo - turns knob.
I'm still curious: would you mind writing what happens if the servo turns the knob to the right / to the left?
I'm pretty sure it does not ignite a space-X-rocket ;-)) but what does it?
best regards
Stefan
StefanL38:
first I want to ask what your code shall do in the end.
Moving the servo 35degrees is not a selfpurpose. there is something "behind" it.
Please asnwer this question. In a lot of cases better working solutions can be found if the real purpose is known.
My guessing is: switching On/Off your automatic climatisation-unit depending on a measured temperature. Is this right?
Yes! this is exactly what I want to accomplish.
Anyway there is a library called ControlledServo which offers additional functionality over the standard-servo-library
Are you already familiar with how to install additional libraries into arduino-IDE?
yes, and done.
OK so you seem to be on rails realising this project servo - turns knob.
I'm still curious: would you mind writing what happens if the servo turns the knob to the right / to the left?
I'm pretty sure it does not ignite a space-X-rocket ;-)) but what does it?
Hahah Turning knob right from origin(off position) will turn on the A/C, turing left will activate the fans only.
GolamMostafa:
Is Step-2 working? Now, think about passing the temperature value as an argument of myServo.write(arg) method. Are you familiar with map() function which you can use to compress the 'int type' temperature value.
Yes! Not really familiar with C++ functions but thanks for the tip. I'll do more reaserch into it
ailun_y:
Turning knob right from origin(off position) will turn on the A/C, turing left will activate the fans only.
OK saving energy if outside-temperature is lower than inside temperature. Just blow in the air without cooling.
You already calculate the temperatures. So I don't see a need for a map function.
In the long run you will progress faster if you test each functionality in an extra program.
So one program for just moving the servo between three positions right - middle - left
one small program for reading in the potentiometer
after that start to add one thing at a time and test again.
In this way the possible reasons why something works not is reduced as much as possible.
If you would put it all together and then start testing there would be a lot of more possible combinations where the bug could be.
(by the way there is a famous wording: "the "BUG" always has its hands on the keyboard and is staring at the screen"
One more tip: I have the habit to use serial-output to the com-port for testing. The serial-monitor has almost infinite lines to write.
I will stay in idle-mode until you came up with new questions
best regards
Stefan
ailun_y:
Yes! Not really familiar with C++ functions but thanks for the tip. I'll do more reaserch into it
1. OK! Your Servo is working.
2. What is the probable range of your temperatureC? Say, it is: 20.500C to 98.500C. Take the integer part of the temperature.
float temperatureC = (voltage - 0.5) * 100 ;
int tempC = (int)temperatureC;
3. What position the Servo should have when the tempC is 200C? Say, it is: 00.
4. What position the Servo should have when the tempC is 980C? Say, it is: 1800.
5. Now map the tempC range with Servo range by map() function.
byte servoPos = map(tempC, 20, 98, 0, 180);
6. Pass servoPos as the argument of Servo.wrtie() method.
myServo,write(servoPos);
7. Check that as the temperature changes, the Servo's shaft position changes accrdinngly/proportionately.