Arduino temp & led - arduino

I have arduino uno r3, temp sensor lm335z and 2 led.
I found this code in internet
float celsius = 0, kelvin=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
kelvin = analogRead(0) * 0.004882812 * 100;
celsius = kelvin - 273.15;
Serial.print("Celsius: ");
Serial.println(celsius);
//Serial.print("Kelvin: ");
//Serial.println(kelvin);
Serial.println();
delay(10000);
}
and works great with this schema
temp only
I add two led with this code:
float celsius = 0, kelvin=0;
int led_green = 13;
int led_red = 12;
void setup()
{
Serial.begin(9600);
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);
}
void loop()
{
kelvin = analogRead(0) * 0.004882812 * 100;
celsius = kelvin - 273.15;
Serial.print("Celsius: ");
Serial.println(celsius);
//Serial.print("Kelvin: ");
//Serial.println(kelvin);
Serial.println();
if (celsius <= 25.00)
{
digitalWrite(led_green, HIGH);
digitalWrite(led_red, LOW);
}
else
{
digitalWrite(led_green, LOW);
digitalWrite(led_red, HIGH);
}
delay(10000);
}
and this schema:
led and temp
Temperature 1,2 or 3 degree plus than normal where or what I miss?

Because of
kelvin = analogRead(0) * 0.004882812 * 100;
So each step of the ADC will imply ~0.5 degrees of temperature difference. Since you did not specify anything in your sketch the voltage reference is the supply voltage. Loading the Arduino's outputs with just one LED (as you do) may affect the supply voltage in the order of magnitude of 50-100 mV. This in turn will affect your temperature reading by several degrees.
You can find a detailed analysis of this effect in my blog
So the issue can be explained by the addition of LEDs to your circuit.

Related

Code for increasing RPM of a DC motor proportionally to the Temperature input in Arduino

So i have this circuit in TinkerCad
that turns the dc motor on and off depending on the input of the temperature in TMP. How do i make it so that instead of turning the DC motor on and off, it instead increases the rpm as the temperature input goes higher? so far i have this code:
int sensorPin = 0;
void setup()
{
pinMode(A0,INPUT);
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
}
void loop()
{
int reading = analogRead(sensorPin);
float voltage = reading * 5;
voltage /= 1024.0;
float celsius = (voltage - 0.5) * 100;
Serial.print(celsius);
Serial.println(" degrees C");
if (celsius < 30) {
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
if (celsius > 30)
{
digitalWrite(11,LOW);
digitalWrite(10, HIGH);
}
delay(10);
}
You can use the analogWrite to send a PWM-Signal to the L293D.
analogWrite(outputPin, map(analogRead(inputPin), 0, 1023, 0, 255); //outputPin needs to be a PWM-Pin
References: analogWrite(), map()

Ultrasonic sensors

Now i am using 2 ultrasonic sensors but i found that they become unstable outdoors and give random readings .....although they act normally inside a room .....how can i solve this problem???
That is the code
it seems like the off Sensor always Reading and i checked the wiring and it is good so i don't really know where is the problem is ??
#define trig 2
#define echo 3
#define led 7
#define relay 4
#define trig2 10
#define echo2 9
#define led2 8
long duration,distance,OnSensor,OffSensor;
void setup() {
// put your setup code here, to run once:
pinMode(echo,INPUT);
pinMode(echo2,INPUT);
Serial.begin(9600);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(trig,OUTPUT);
pinMode(trig2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led,LOW);
digitalWrite(led2,LOW);
digitalWrite(relay,LOW);
SonarSensor(trig2, echo2);
OffSensor = distance;
delay(50);
SonarSensor(trig, echo);
OnSensor=distance;
if(OnSensor<80&&OffSensor>=80){//sensor 1 activated
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
delay(1000);
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor>80){//No sensor activated
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor<80){//sensor2 activated
digitalWrite(led2,HIGH);
digitalWrite(relay,LOW);
delay(5000);
}
else if(OnSensor<80&&OffSensor<80){//Both sensors activated
digitalWrite(led2,HIGH);
digitalWrite(led,HIGH);
digitalWrite(relay,LOW);
delay(3000);
}
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
I think there are 2 problem here
You use 2 sensor
List item
Use use it outdoor
From my experience the ultra sonic wave can bouncing around object many time. depend on your setup, second sensor might read echo form first sensor.
follow these step
1. Try to use 1 sensor per time to see if it error
2. If single sensor work you should add more delay after sensor reading
3. Plot the data to see how it look like you might need digital filter to improve reading
For step 3 if you use sensor as On Off by threshold you can see my example code.
This code work the same way as button de-bouncing so in need to trig continuously for 10 time to prevent false alarm (This code for single sensor)
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define RELAY_PIN 4
/**/
#define ULTRASONIC_INTERVAL_MS 100//50
#define ULTRASONIC_TRIG_COUNT_THRESH 10
#define ULTRASONIC_DISTACE_THRESH_CM 50
/**/
#define RELAY_TRIG__INTERVAL_MS 5000
#define RELAY_TRIG_LOGIC HIGH
void setup()
{
pinMode (RELAY_PIN, OUTPUT);
pinMode (ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode (ULTRASONIC_ECHO_PIN, INPUT);
Serial.begin(115200);
}
void loop()
{
static unsigned long ultrasonic_timer = millis();
static unsigned long relay_trig_timer = millis();
static float us_distance_cm = 0;
static int us_trig_count = 0;
static byte relay_state = !RELAY_TRIG_LOGIC; // initial value as off;
if (millis() - ultrasonic_timer >= ULTRASONIC_INTERVAL_MS) {
ultrasonic_timer += ULTRASONIC_INTERVAL_MS;
us_distance_cm = getDistanceCM();
if (us_distance_cm <= ULTRASONIC_DISTACE_THRESH_CM) {
if (us_trig_count >= ULTRASONIC_TRIG_COUNT_THRESH) {
digitalWrite(RELAY_PIN, RELAY_TRIG_LOGIC);
relay_state = RELAY_TRIG_LOGIC;
relay_trig_timer = millis();
}
else {
us_trig_count++;
}
}
else {
us_trig_count = 0;
}
Serial.print("distance = ");
Serial.print(us_distance_cm);
Serial.print("cm, relay = ");
Serial.print(relay_state);
Serial.print(", count = ");
Serial.println(us_trig_count);
}
if (relay_state == RELAY_TRIG_LOGIC) {
if (millis() - relay_trig_timer > RELAY_TRIG__INTERVAL_MS) {
relay_state = !RELAY_TRIG_LOGIC;
digitalWrite(RELAY_PIN, !RELAY_TRIG_LOGIC);
}
}
}
float getDistanceCM() {
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
unsigned long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH, 50000);
float distance = float(duration * 343) / 20000.0;
delay(5);
return distance;
}

Arduino fan controller code issues (LM35)

I got an issue with the PWM signal on the fan, it actually gets to 100% right away when it hits 21°C when it should be on 10%. I don't think it's a circuit issue, so any suggestions on the code ? I am pretty sure the problem is somewhere at the Map function, just can't seem to figure it out.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1; // the output pin of LM35
int fan = 11; // the pin where fan is
int led = 8; // led pin
int temp;
int tempMin = 20; // the temperature to start the fan
int tempMax = 30; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
void setup() {
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
void loop() {
temp = readTemp(); // get the temperature
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
fanSpeed = map(temp, tempMin, tempMax, 115, 255); // the actual speed of fan
fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
if(temp > tempMax) { // if temp is higher than tempMax
digitalWrite(led, HIGH); // turn on led
} else { // else turn off led
digitalWrite(led, LOW);
}
lcd.print("TEMP: ");
lcd.print(temp,1); // display the temperature
lcd.print("C ");
lcd.setCursor(0,1); // move cursor to next line
lcd.print("FAN: ");
lcd.print(fanLCD); // display the fan speed
lcd.print("%");
delay(500);
lcd.clear();
}
int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return (temp * 0.48828125)-48;
}
Ok so apparently there was a problem with this specific pin, i tried another one (PWM), and it worked perfectly!

Arduino temperature sensor counting back

I am trying to get my new velleman vma320 to work with my arduino.
It doensn't work at all, the temperature goes down its heated up. I've tried everything. Can somebody help me? Here is my code...
int SensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
//reading
int sensorvalue = analogRead(SensorPin);
Serial.print("value: ");
Serial.print(sensorvalue);
//voltage
float voltage = sensorvalue * 5.0;
voltage /= 1024.0;
Serial.print(", volts: ");
Serial.print(voltage);
//temperature
float temperature = (voltage - 0.5) * 100 ;
Serial.print(" degrees C");
Serial.println(temperature);
}
Is it something I have done wrong? Or is it just the sensor? I tried it with two sensors.
If you can help me that would be awesome.
Thanks in advance,
Jens Van den Eede.
So, this is the working code for a thermister velleman vma320. According to the way it's wired, voltage will go down as the temperature goes up, and it's not linear.
#include <math.h>
double Thermistor(int RawADC) {
double Temp;
Temp =log(10000.0/(1024.0/RawADC-1)); // for pull-up configuration
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(int(Thermistor(analogRead(A0))));
delay(1000);
}
Please note the above code only provides accurate temperatures if you are supplying the VMA320 with 3.3VDC (from VCC, not 5V).
Also add "Temp = (Temp * 9.0)/ 5.0 + 32.0;" above "return Temp;" if you wish to convert to °F

no breadboard LED output

I'm making a simple temperature sensor to light one of two LEDs depending on the temperature.
For some reason the LED output only blinks the onboard LED (pin 13 on the Edison) once.
My temperature output is working fine, but I'm not sure why my code is working incorrectly.
Photo of the wiring here.
int temppin = 0;
int ledhigh = 7;
int ledlow = 8;
void setup()
{
Serial.begin(9600);
pinMode(temppin, INPUT);
pinMode(ledhigh, OUTPUT);
pinMode(ledlow, OUTPUT);
}
void loop()
{
int tempout = analogRead(temppin);
float volts = tempout * 5.0;
volts /= 1024.0;
float temp = (volts - 0.5) * 100 ;
Serial.print(temp); Serial.println(" celsius");
if (temp > 0){
Serial.print("high temp =");
digitalWrite(ledhigh, HIGH);
} else {digitalWrite(ledlow, HIGH);
Serial.print("low temp");
}
delay(3000);
}
The problem is probably that you're trying to use the analog input pins as output. You need to use the digital pins.
As explained in this video:
https://youtu.be/BtLwoNJ6klE?t=50s

Resources