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
Related
I have a problem with this circuit... when i Send a value from Serial Monitor in Arduino it doesn't receives the value and continues in an infinite loop. So leds don't switch on and I don't understand why.
Here below there is the image of the circuit:
This is the code:
const int sensorPin = A0;
const float baselineTemp = 20.0;
void
setup ()
{
Serial.begin (9600);
for (int pinNumber = 2; pinNumber < 5; pinNumber++)
{
pinMode (pinNumber, OUTPUT);
digitalWrite (pinNumber, LOW);
}
}
void
loop ()
{
int sensorVal = analogRead (sensorPin);
Serial.print ("Sensor Value: ");
Serial.print (sensorVal);
//converti la temperatura ADC in tensione
float voltage = (sensorVal / 1024.0) * 5.0;
Serial.print (", Volts: ");
Serial.print (voltage);
Serial.print (", degrees: ");
//converti la tensione in temperatura
float temperature = (voltage - 5) * 100;
Serial.println (temperature);
}
Leds don't switch on and serial monitor doesn't receive the values but it gives me this loop:
Thanks in advance
You’ll need to implement some code to turn on the leds in the loop like:
digitalWrite(pinNumber, HIGH)
Your question is not clear. When do you want the LED switches on?
You just read values from the serial line and print them on the serial monitor.
you have to use:
Serial.read();
to get data from the serial monitor and after that decide how the LED changes behavior. Also, you need to use
digitalWrite(pinNumber, HIGH);
to set LED pins high and then base on your condition (get some specified data from Serial or a temperature limit) turn them off by:
digitalWrite(pinNumber, LOW);
This is the code I wrote:
I set the pins given accordingly I checked my ultrasonic sensors, transistor, and resistors are in working condition. CS (in code denoted as chipset) is pinned in pin 10, resistor to 9, and that resistor is connected to the transistor, and then it is connected to the speaker.
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8
#define cs 10
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
void setup()
{
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(cs))
{
Serial.println("SD fail");
}
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}
void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
tmrpcm.setVolume(6);
if (RightSensor > 10)
{
tmrpcm.play("RightFormatted.wav");
}
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
if (LeftSensor > 10)
{
tmrpcm.play("LeftFormatted.wav");
}
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;
if (FrontSensor > 10)
{
tmrpcm.play("FrontFormatted.wav");
}
Serial.println(LeftSensor);
Serial.println(" - ");
Serial.print(FrontSensor);
Serial.print(" - ");
Serial.println(RightSensor);
}
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 / 2) / 29.1;
}
The error I am getting is:
Error of this code
Instead of typing the name of the .wav file directly inside the .play method, create a const char variable to assign the name(s):
const char* audio1= "RightFormatted.wav";
then you can use it as:#
tmrpcm.play(audio1);
However, based on the examples from the library, you do not need the file extension, but I might be wrong in this regard.
Try first my proposal and please report your results
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!
I am working on an LED display project and trying to show some words on the display, though I can't get continuous lines to light up. Below is the code.
int greenPin = 7;
int redPin = 6;
int stbPin = 2;
int clkPin = 3;
int aPin = 4;
int bPin = 5;
int delayTime = 1;
int i = 0;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(stbPin, OUTPUT);
pinMode(clkPin, OUTPUT);
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
digitalWrite(stbPin, HIGH);
digitalWrite(clkPin, LOW);
}
void loop() {
digitalWrite(stbPin, LOW);
digitalWrite(aPin, HIGH);
digitalWrite(bPin, LOW);
twoLines(B11111111, B11111111, B00000000, B00000000, B11111111, B11111111, B00000000, B00000000);
digitalWrite(aPin, LOW);
digitalWrite(bPin, LOW);
delayMicroseconds(delayTime);
digitalWrite(stbPin, HIGH);
delayMicroseconds(delayTime);
}
void twoLines( byte br, byte dr, byte ar, byte cr, byte bg, byte dg, byte ag, byte cg) {
byte Garr[] = { ag, bg, cg, dg };
byte Rarr[] = { ar, br, cr, dr };
for ( int i = 0; i < 4; i++ ) {
for (byte mask = 11111111; mask > 0; mask >>= 1) {
digitalWrite(clkPin, LOW); // delayMicroseconds(delayTime);
if (Rarr[i] & mask) {
digitalWrite(redPin, HIGH);
} else {
digitalWrite(redPin, LOW);
}
if (Garr[i] & mask) {
digitalWrite(greenPin, HIGH);
} else {
digitalWrite(greenPin, LOW);
}
delay(2);
digitalWrite(clkPin, HIGH);
delay(2);
}
}
}
Right now, I can at most only light up two rows, but I want the capability to be able to light up three rows at once to create letters. Help of any kind is appreciated. Below is also how the LED display looks like.
For things like this first thing you do, read the data sheet of your LED and see how much current they draw. If you got like 100 LED and each draw 20 mA, that would be 2 Amps and not only Arduino can not supply it, you are risking burning your Arduino. After you figured out how much current your system needs, you gotta use a power source that can handle it. You got several options, for example a BJT is a current amplifier but it complicates the design and needs some background in electrical engineering. The other option would be using external power sources such as batteries or wall plugs. Then, you just need to buy a good adapter or voltage regulator that can handle your current need. Also, in that case you will need some sort of relay to control the leds with your Arduino. What Relay does is that it lets you turn it on and off with micro controller logic, but when it is on it uses an external power source.
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.