Project 3 issues!

Hello. Today I began doing some Arduino projects (I love it thus far!) and it went well. However, during project 3, one of my LEDs doesn't glow much. I'll post the code, hopefully that will help you understand my issue.

const float baselineTempt = 28.70;
  
void setup(){

 
 Serial.begin(1200);
  
  for(int pinNumber = 2; pinNumber < 5; pinNumber++){
  
  pinMode(pinNumber, OUTPUT);
  pinMode(pinNumber, LOW);
  }  
}

void loop(){
  
 int sensorVal = analogRead(A0);
 float voltage = (sensorVal/1024.0) * 5.0;
 float temperature = (voltage - .5) * 100;

  
Serial.print("Sensor value: ");
Serial.println(sensorVal);
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.print("degrees C: ");
Serial.println(temperature);
Serial.println("    ");

if(temperature <= baselineTempt){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}
else if(temperature < (baselineTempt + 2) && temperature > baselineTempt){
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}
else if(temperature >= (baselineTempt + 2) && temperature < (baselineTempt + 4)){
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
}
else if(temperature >= baselineTempt + 4){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  
}
delay(1);
}

Basically, the OUTPUT port 3 LED won't glow much. I feel that there is not enough voltage or something. Even if I try without a resistor, it barely glows.

So, question 1: Do you know why it (the output 3 port) doesn't glow? I don't think it's a code issue. ||EDIT|| I just noticed that the output port 3 is reserved for PMW. Could this perhaps explain why it glows so vaguely? If this is the case, can I change it to a static 5V output?

Question 2: The temperature sensor (TMP36) feels sort of dull.. I have to press it hard for it to increase it's temperature, almost too hard. I know it's not high-tech but still, I have nothing to compare it with. Should it be smoother or is it working as intended?

Thanks in advance.

That appears to be a bad LED. Did you try swapping it with another one?

Output port 3 will only do PWM with an analogWrite() command. You are using digitalWrite() commands so PWM does not apply here.

You have the baselineTempt is set high (28.7 C). Unless it's very warm where you are, that could be why you have a harder time increasing the temperature. The baselineTempt should be changed to your local room temperature.

Hello, thx for replying.

There is nothing wrong with the LED, I tried 7-8 different ones.

As for my baselineTempt, I set it to the temperature that my sensor measured. Of course my room temperature is not 28.7 C, but if I set the baselineTempt to 22 C or so, it would pretty much always stay in the same mode.

It may be a possibility that your output port 3 is damaged. It can happen by drawing too much current from a port. LEDs should always be used with at least a 220 Ohm or greater resistor. You should avoid using it directly to a port. This could cause a port to burn out. Depending on the type and color of an LED the resistance can vary a bit. I typically try to use at least a 1K Ohm resistor to be safe. We may want to try some other experiments to test if your port is working correctly or not.

As for the TMP36, make sure you have the +5V and GND connections correct. With the TMP36 "flat-side" facing you, the power +5V should be on the left pin-1, the middle pin-2 is always the signal going to the analog input on the Arduino, the ground GND should be on the right pin-3.

The port is not damaged, it works well when I'm doing other projects.. Ahwell, I can try looking into it when I'm more experienced!

You seem to know a whole lot, so do u mind if I ask why how the analogWrite function works?

I wanted to make a few LEDs shine with different brightness, this is part of the code:

analogWrite(3, 30);
 analogWrite(5, 60);
 analogWrite(6, 90);
 analogWrite(9, 120);

However, I can't see any brightness-level difference. Do you know why?

Thanks!

Did you setup the ports as output ports in your setup()?

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
}

Thx again for replying.

Yes I have done that.

Here is the full code.

void setup(){
  
 pinMode(2, INPUT);
 pinMode(3, OUTPUT); 
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT); 
 
}
 
 
 
void loop(){
  
  int button = 0;
  
  button = digitalRead(2);

 
  if(button == HIGH){
    
    analogWrite(3, 30);
    delay(75);
    analogWrite(5, 60);
    delay(75);
    analogWrite(6, 90);
    delay(75);
    analogWrite(9, 120);
    delay(75);
    analogWrite(10, 150);
    delay(75);
    analogWrite(11, 180);
    delay(75);
    analogWrite(12, 210);
    delay(75);
    analogWrite(3, 0);
    delay(100);
    digitalWrite(5, LOW); // I used digitalWrite here because I can't spot the difference anyways.
    delay(75);
    digitalWrite(6, LOW);
    delay(75);
    digitalWrite(9, LOW);
    delay(75);
    digitalWrite(10, LOW);
    delay(75);
    digitalWrite(11, LOW);
    delay(75);
    digitalWrite(12, LOW);
    delay(75);
    
    
    
}
else{
    digitalWrite(3, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  
}
}

Can you try this code and see if you can see the LEDs vary brightness?

void setup(){
  
 pinMode(2, INPUT);
 pinMode(3, OUTPUT); 
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT); 
 
}
 
void loop(){
  int button = 0;
 
  button = digitalRead(2);

  if(button == HIGH){
    
    GlowLightOnce(3);
    GlowLightOnce(5);
    GlowLightOnce(6);
    GlowLightOnce(9);
    GlowLightOnce(10);
    GlowLightOnce(11);
    
 } else{
    analogWrite(3, LOW);
    analogWrite(5, LOW);
    analogWrite(6, LOW);
    analogWrite(9, LOW);
    analogWrite(10, LOW);
    analogWrite(11, LOW);
  }
}

void GlowLightOnce(int bulb)  // subroutine applies one fade in - fade out blink of an LED
{
  int brightness = 0;
  int fadeAmount = 5;
  int totalcount = 0;
    do
    {
      analogWrite(bulb, brightness);
      brightness = brightness + fadeAmount;
      totalcount++;
      if (brightness == 255)
      {
        fadeAmount = -fadeAmount;
      }
      delay(35);
    } while (totalcount < 103);
}