• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C and Arduino help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
That means there's a short somewhere in the thermister circuit and no voltage is getting to the Arduino.

What I would do is thusly:
Write a quick and simple analogRead-serial sketch to see what's happening on that pin
Code:
void setup(){
  Serial.begin
}

void loop(){
  Serial.println(analogRead(0));
  delay(10);
}
Obviously you need to set the pin being read to whatever the thermister is connected to.
Then you can play with the circuit till you get reasonable values out of it.


OK, I'm going to be perfectly honest here...
I forgot that I disconnected the 5V a while ago....so I didn't accidentally blow up any LEDs or anything lol.

OK now I'm getting readings....time to try my program.
 
nothing but good news......
The LEDs are working.
The speaker is sounding when it goes above 20 degrees (which I'll change to something more appropriate).
The LDR is working too.....I can make the whole thing stop by putting my hand over it.

Next I'll try the fan and then it's just button presses and the potentionmeter. I don't really understand what the potentiometer's supposed to do so I might email the tutor...

Thanks a lot for your help so far! We're getting there...
 
rofl that'd explain the zero voltage reading!
Don't feel bad, anyone who's done some electronics stuff has done that too :D
Awesome to hear it's working now!

I'm assuming the pot is for adjusting the alarm and/or fan points. Maybe the LDR trigger point?

Can't say I know either.


Anyway, good work!
 
OK I'm having trouble with the fan

Code:
/*
* Carl White ICT106 Project 01
* Arduino climate control device
*
* 3.it needs to activate a fan through a transistor after a certain temp is reached
* 4.Use different fan speeds depending on temp
* 5.Implement an alarm reset system using a code from several button presses
* 7.Use a potentiometer as a calibration mechanism
* 8.Print out an array of info when a button is pressed.
*/

#include <math.h>
const int thermPin = 0;         // Use Pin 0 for thermistor analog input
      int potenPin = 1;
      int ldrPin = 2;
      int buttonPin6 = 6;
      int buttonPin7 = 7;
      int speakerPin = 9;
      int ledPin10 = 10;
      int ledPin11 = 11;
      int ledPin12 = 12;
      int fanPin = 13;
float vcc = 5;
float balance = 10000;
float thermr = 10000;
long temp;





//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int thermPin){
  pinMode(thermPin, INPUT);
  long Resistance;
  float Temporary;
  long temptemp;
  temptemp = analogRead(thermPin);
  Resistance = ((1024 * balance / temptemp) - balance); 
  Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
  Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
  Temporary = Temporary - 273.15;  // Convert Kelvin to Celsius
  temp = Temporary;
  return temp;                 // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"




//Fan Function
void fanSpin(){
  pinMode(fanPin, OUTPUT);
  int range;
  range = map(temp, 0, 50, 0, 3);
  
  switch (range) {
        case 0:  //fan off
    Serial.println("fan off");
    digitalWrite(fanPin, LOW);
    
        case 1:  //slowest speed
    Serial.println("slowest fan speed");
    digitalWrite(fanPin, HIGH);
    delay(750);
    digitalWrite(fanPin, LOW);
    delay(750);
    
        case 2:  //medium speed
    Serial.println("medium fan speed");
    digitalWrite(fanPin, HIGH);
    delay(500);
    digitalWrite(fanPin, LOW);
    delay(500);
    
        case 3:  //fastest speed
    Serial.println("fastest fan speed");
    digitalWrite(fanPin, HIGH);
    delay(250);
    digitalWrite(fanPin, LOW);
    delay(250);
  }
}




//Lighting LEDs based on temp//
void LEDs(){
  //Declare the LED pins as output
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  
  int range;
  range = map(temp, 0, 50, 0, 3);

  switch (range) {
  case 0:    // no LEDs
    Serial.println("All LEDs off");
    digitalWrite(ledPin10, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 1:    // 1 LED
    Serial.println("First LED on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 2:    // 2 LEDs
    Serial.println("First 2 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, LOW);
    break;
  case 3:    // 3 LEDs
    Serial.println("All 3 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, HIGH);
    break;
 }
 }
//End of LED function




//Sounding an Alarm based on temp
void alarmSound(){
  pinMode(speakerPin, OUTPUT);
  long frequency = 523;  //C5 tone freq in Hz
  long length = 1000; //the amount of time it's going to sound for - 1 sec
  
  long delayValue = 1000000/frequency/2;
  
  long numCycles = frequency * length/1000;
  
  if (temp > 20){
  
  for (long i = 0; i < numCycles; i++){
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayValue);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayValue);
  }
  delay(100); //delay for 1 second before repeating
}
else
{
  Serial.println("Not hot enough for alarm");
}
}
//End of Alarm function





//Reading the light level from the LDR
//void lightLevel()


  
//End of light level function





void setup() {
     Serial.begin(9600);
}

void loop() {
 int lightReading = analogRead(ldrPin);  //read the LDR raw value
Serial.print(lightReading);
Serial.println(" LDR raw value ");
  if(lightReading > 1){
    
    
//Do the entire rest of the program, if the lights are ON
  float temp;
  temp=Thermistor(thermPin);       // read ADC, convert it to Celsius and put it into the "temp" variable
  LEDs();
  Serial.print(temp);
  Serial.print(" degrees Celcius");
  Serial.println("\n");
  delay(1000);
  alarmSound();
  fanSpin();
    }
    
    
//Don't do anything if the lights are off
 else{
 Serial.println("Operation halted due to lack of light");
 digitalWrite(ledPin10, LOW);
 digitalWrite(ledPin11, LOW);
 digitalWrite(ledPin12, LOW);
 digitalWrite(speakerPin, LOW);
 digitalWrite(fanPin, LOW);
 delay(1000);
 }
}

and my serial monitor:
Not hot enough for alarm
slowest fan speed
medium fan speed
fastest fan speed
27 LDR raw value
First LED on
19.00 degrees Celcius

It's cycling through each case in the fan function? Not just choosing one? And regardless, the fan's not spinning....I'm 99% sure it's connected up right....


EDIT: I forgot the "break;" after each!!
solved that part of it......but the fan still isn't spinning.

EDIT AGAIN: I think I had the transistor around the wrong way, now it's spinning a tiny little bit back....then a tiny bit forth!
 
Last edited:
I would use PWM for the fan speed, analogWrite is the thing to look into there.

How do you have the fan wired?
 
I would use PWM for the fan speed, analogWrite is the thing to look into there.

How do you have the fan wired?

Cool, I'll look into that tomorrow.
It's wired with a transistor as we were shown. One leg to ground, one the input wire and the other to the fan's + terminal....then obviously the fan's - terminal to ground.
 
Code:
/*
* Carl White ICT106 Project 01
* Arduino climate control device
* 5.Implement an alarm reset system using a code from several button presses
* 7.Use a potentiometer as a calibration mechanism
* 8.Print out an array of info when a button is pressed.
*/

#include <math.h>
const int thermPin = 0;         // Use Pin 0 for thermistor analog input
      int potenPin = 1;
      int ldrPin = 2;
      int buttonPin6 = 6;
      int buttonPin7 = 7;
      int speakerPin = 9;
      int ledPin10 = 10;
      int ledPin11 = 11;
      int ledPin12 = 12;
      int fanPin = 13;
float vcc = 5;
float balance = 10000;
float thermr = 10000;
long temp;





//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int thermPin){
  pinMode(thermPin, INPUT);
  long Resistance;
  float Temporary;
  long temptemp;
  temptemp = analogRead(thermPin);
  Resistance = ((1024 * balance / temptemp) - balance); 
  Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
  Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
  Temporary = Temporary - 273.15;  // Convert Kelvin to Celsius
  temp = Temporary;
  return temp;                 // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"




//Fan Function
void fanSpin(){
  pinMode(fanPin, OUTPUT);
  int range;
  range = map(temp, 0, 50, 0, 3);
  
  switch (range) {
        case 0:  //fan off
    Serial.println("fan off");
    break;
        case 1:  //slowest speed
    Serial.println("slowest fan speed");
    analogWrite(fanPin, 255);
    delay(750);
    analogWrite(fanPin, 0);
    delay(750);
    break;
        case 2:  //medium speed
    Serial.println("medium fan speed");
    analogWrite(fanPin, 255);
    delay(500);
    analogWrite(fanPin, 0);
    delay(500);
    break;
        case 3:  //fastest speed
    Serial.println("fastest fan speed");
    analogWrite(fanPin, 255);
    delay(250);
    analogWrite(fanPin, 0);
    delay(250);
    break;
  }
}




//Lighting LEDs based on temp//
void LEDs(){
  //Declare the LED pins as output
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  
  int range;
  range = map(temp, 0, 50, 0, 3);

  switch (range) {
  case 0:    // no LEDs
    Serial.println("All LEDs off");
    digitalWrite(ledPin10, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 1:    // 1 LED
    Serial.println("First LED on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 2:    // 2 LEDs
    Serial.println("First 2 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, LOW);
    break;
  case 3:    // 3 LEDs
    Serial.println("All 3 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, HIGH);
    break;
 }
 }
//End of LED function




//Sounding an Alarm based on temp
void alarmSound(){
  pinMode(speakerPin, OUTPUT);
  long frequency = 523;  //C5 tone freq in Hz
  long length = 1000; //the amount of time it's going to sound for - 1 sec
  
  long delayValue = 1000000/frequency/2;
  
  long numCycles = frequency * length/1000;
  
  if (temp > 20){
  
  for (long i = 0; i < numCycles; i++){
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayValue);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayValue);
  }
  delay(100); //delay for 1 second before repeating
}
else
{
  Serial.println("Not hot enough for alarm");
}
}
//End of Alarm function





//Reading the light level from the LDR
//void lightLevel()


  
//End of light level function





void setup() {
     Serial.begin(9600);
}

void loop() {
 int lightReading = analogRead(ldrPin);  //read the LDR raw value
Serial.print(lightReading);
Serial.println(" LDR raw value ");
  if(lightReading > 1){
    
    
//Do the entire rest of the program, if the lights are ON
  float temp;
  temp=Thermistor(thermPin);       // read ADC, convert it to Celsius and put it into the "temp" variable
  LEDs();
  Serial.print(temp);
  Serial.print(" degrees Celcius");
  Serial.println("\n");
  delay(1000);
  alarmSound();
  fanSpin();
    }
    
    
//Don't do anything if the lights are off
 else{
 Serial.println("Operation halted due to lack of light");
 digitalWrite(ledPin10, LOW);
 digitalWrite(ledPin11, LOW);
 digitalWrite(ledPin12, LOW);
 digitalWrite(speakerPin, LOW);
 digitalWrite(fanPin, LOW);
 delay(1000);
 }
}

I changed things to analogWrite but it still just moves the fan a tiny bit.
I've been reading a bit and do I need to use a capacitor or something?
 
PNP transistor or NPN transistor?

Generally you use an NPN, in which case it goes between the fan's - wire and ground, and you'll need a resistor (200ohm or so) between the Arduino and ground.

If it's a PNP, then it's wired correctly.


Is the fan getting 5v or 12v? 5v fan or 12v fan?
 
Yup that's a 5v fan. How bout the transistor? PNP (P channel) or NPN (N channel)?
 
That may explain why the fan isn't working.

Step1) Plug fan directly to 5v and GND and make sure it works.

Step2) Wire fan+ to 5v, fan- to the mosfet Drain pin, mosfet Source to GND, then attach the mosfet Gate pin to 5v and see what happens, then GND and see what happens.

That mosfet isn't a "logic level" device, so it expects more like 10v to fully turn on, that may be the issue. Mosfets are kind of odd devices that way.
Did it come from the school?
 
im a little late to the party but ill see if i cant help ya out.

it looks to me your building a Common Source amplifier to power your fan.

according to your data sheet your n-channel (NPN - Not Pointing iN) mosfet has a V(gate to source) threshold voltage of 2.0V. this means for your current to flow from your drain to source you must have a voltage difference of at least 2 volts measured from the gate to source.

the drain current equation, the equation that tells you how much current flows from your drain to source is:

Id = 1/2*mu*Cox*w/l * (Vgs -Vth)^2

you shouldn't have to concern yourself with those parameters beyond Id, Vgs and Vth(reshold). so basically increase Vgs bigger than 2V and you get more current coming down the drain and a faster spinning fan.

of course your fan acts as a resistance but to be safe place a resistor from your voltage rail (5-10 volts) to the drain of the transistor.

lastly always remember no matter what kind of amplifier you're using you cannot amplify past your rail voltage. in other words if you put your fan at Vout (tied to the drain node) at max you can see 5V if your rail is 5V (most micros have a 3.3 and 5v rails, i think the arduino setup is no exception).

i hope this helps some :) good luck to ya!

a few other notes, your micro probably has a max current draw allowed, if your fan or power transistor are pulling more than that you need an external supply to power those parts
 
Last edited:
That may explain why the fan isn't working.

Step1) Plug fan directly to 5v and GND and make sure it works.

Step2) Wire fan+ to 5v, fan- to the mosfet Drain pin, mosfet Source to GND, then attach the mosfet Gate pin to 5v and see what happens, then GND and see what happens.

That mosfet isn't a "logic level" device, so it expects more like 10v to fully turn on, that may be the issue. Mosfets are kind of odd devices that way.
Did it come from the school?

im a little late to the party but ill see if i cant help ya out.

it looks to me your building a Common Source amplifier to power your fan.

according to your data sheet your n-channel (NPN - Not Pointing iN) mosfet has a V(gate to source) threshold voltage of 2.0V. this means for your current to flow from your drain to source you must have a voltage difference of at least 2 volts measured from the gate to source.

the drain current equation, the equation that tells you how much current flows from your drain to source is:

Id = 1/2*mu*Cox*w/l * (Vgs -Vth)^2

you shouldn't have to concern yourself with those parameters beyond Id, Vgs and Vth(reshold). so basically increase Vgs bigger than 2V and you get more current coming down the drain and a faster spinning fan.

of course your fan acts as a resistance but to be safe place a resistor from your voltage rail (5-10 volts) to the drain of the transistor.

lastly always remember no matter what kind of amplifier you're using you cannot amplify past your rail voltage. in other words if you put your fan at Vout (tied to the drain node) at max you can see 5V if your rail is 5V (most micros have a 3.3 and 5v rails, i think the arduino setup is no exception).

i hope this helps some :) good luck to ya!

a few other notes, your micro probably has a max current draw allowed, if your fan or power transistor are pulling more than that you need an external supply to power those parts

And then there were three! Welcome to the party whooping_a_panda! (great name too lol).

OK I got it figured out by Bob's instructions....different wiring of the transistor. Everything whooping_a_panda said about current, voltage and resistance etc. seems smart....I wish I understood it! It's now working without any resistors in the circuit so I'm gonna just leave it.

The only issue now is my fan code
Code:
//Fan Function
void fanSpin(){
  pinMode(fanPin, OUTPUT);
  int range;
  range = map(temp, 0, 30, 0, 3);
  
  switch (range) {
        case 0:  //fan off
    Serial.println("fan off");
    break;
        case 1:  //slowest speed
    Serial.println("slowest fan speed");
    analogWrite(fanPin, 75);
    delay(500);
    analogWrite(fanPin, 0);
    delay(500);
    break;
        case 2:  //medium speed
    Serial.println("medium fan speed");
    analogWrite(fanPin, 150);
    delay(500);
    analogWrite(fanPin, 0);
    delay(500);
    break;
        case 3:  //fastest speed
    Serial.println("fastest fan speed");
    analogWrite(fanPin, 255);
    delay(500);
    analogWrite(fanPin, 0);
    delay(500);
    break;
  }
}

It spins, then stops....then spins....then stops etc.
I tried changing the analogWrite values to numbers less than 255 but that doesn't seem to make it go at all. How does one make it run at varying speeds without this pulsing on and off effect?

EDIT: yep the fan is from uni.
Also I just got word back from the tutor....he was cryptic about it but I think the potentiometer is to change the sensitivity of the LDR.
 
Last edited:
analogWrite(fanPin,0); will turn the fan off, so it's doing exactly what you're telling it to :D

Put an analogWrite(fanPin,0); in the "Fan Off" section.
Take it out of the rest of the sections. Take the delays out, too. No need to delay that I can see at least.
The output pin will put out whatever you tell it to until you tell it to do something else, so if it writes 150 to the pin (bit over half, 127 is technically half) it will continue to do so until it is told to write something else to it.
Rather nice, as that means it will simply sit at whatever speed while the Arduino checks the environment or whatever else it's doing.
Downside: When you want it to sleep due to no light, you'll need to put a line in to shut the fan down if it's running. Having an analogWrite(fanPin,0); outside the if(LDR > whatever) statement will do that for you.
Or you can be snazzy and under the closing } for the if statement you can put
Code:
else{
  analogWrite(fanPin,0);
}
That way you aren't turning the fan off at the end of the pass through your loop, only to turn it back on again during the next loop.


Party!

As a total sidenote, a resistor somewhere in the 100-470 ohm range between the Arduino and the Mosfet gate pin is a good idea. Not really needed at the standard PWM speeds (500Hz), but very much required at high speeds and a good habit to get into.
Were it a straight transistor (rather than a mosfet) having no resistor would blow the Arduino to bits (ok, would kill the pin).
 
analogWrite(fanPin,0); will turn the fan off, so it's doing exactly what you're telling it to :D

Put an analogWrite(fanPin,0); in the "Fan Off" section.
Take it out of the rest of the sections. Take the delays out, too. No need to delay that I can see at least.
The output pin will put out whatever you tell it to until you tell it to do something else, so if it writes 150 to the pin (bit over half, 127 is technically half) it will continue to do so until it is told to write something else to it.
Rather nice, as that means it will simply sit at whatever speed while the Arduino checks the environment or whatever else it's doing.
Downside: When you want it to sleep due to no light, you'll need to put a line in to shut the fan down if it's running. Having an analogWrite(fanPin,0); outside the if(LDR > whatever) statement will do that for you.
Or you can be snazzy and under the closing } for the if statement you can put
Code:
else{
  analogWrite(fanPin,0);
}
That way you aren't turning the fan off at the end of the pass through your loop, only to turn it back on again during the next loop.


Party!

As a total sidenote, a resistor somewhere in the 100-470 ohm range between the Arduino and the Mosfet gate pin is a good idea. Not really needed at the standard PWM speeds (500Hz), but very much required at high speeds and a good habit to get into.
Were it a straight transistor (rather than a mosfet) having no resistor would blow the Arduino to bits (ok, would kill the pin).


OK I changed my fan function and loop....
Here's the fan function:
Code:
//Fan Function
void fanSpin(){
  pinMode(fanPin, OUTPUT);
  int range;
  range = map(temp, lowTemp, highTemp, 0, 3);
  
  switch (range) {
        case 0:  //fan off
        analogWrite(fanPin, 0);
    Serial.println("fan off");
    break;
        case 1:  //slowest speed
    Serial.println("slowest fan speed");
    analogWrite(fanPin, 100);
    break;
        case 2:  //medium speed
    Serial.println("medium fan speed");
    analogWrite(fanPin, 175);
    break;
        case 3:  //fastest speed
    Serial.println("fastest fan speed");
    analogWrite(fanPin, 255);
    break;
  }
}

The slowest speed doesn't work at all! (The fan doesn't switch on at all). The medium and Fastest seem to be both exactly the same!

Do I need that resistor now or something? The analog values seem boolean or something......it's either on or off, no variable speed...
 
What pin is your fan connected to? If it's not PWM pin (marked with a - or a ~ on Arduino boards) that's the issue, it needs to be on a PWM pin to use PWM.
Forgot to mention that before.
 
What pin is your fan connected to? If it's not PWM pin (marked with a - or a ~ on Arduino boards) that's the issue, it needs to be on a PWM pin to use PWM.
Forgot to mention that before.

That would be the problem....I'll fix that in a minute.
Then on to the potentiometer and pushbuttons...



EDIT: I tried fanPin as 5 and 6....neither works at slowest speed or medium speed (which I set to 230 and 250 respectively). However again, at 255 it turns on fiercely. I can feel the fan vibrating but not spinning at slowest....and honestly at medium it doesn't seem to be vibrating as much.

Probably a stupid question but there's a 3.3V pin....can we somehow add that to the 5V to give it more potential?
 
Last edited:
2j4vtzt.jpg

A picture of my setup.

You can see the black and red (fan) wires coming in from the right side. The black one was actually one line too far across when I took the pic but I've fixed that. Red goes to the 5V rail, black to the left side of the transistor.

Yellow wire from digital pin 5 to the middle of the transistor.

Little blue wire diagonally to the ground rail.

I'm still having trouble with slow and medium speed....
 
Mosfets have a different pinout generally speaking than normal transistors, the Gate pin is on the left in that picture, Drain is the in the middle, and Source is on the right.
Roughly translated: Left pin goes to the Arduino, middle to the fan black wire, and right to GND.
Despite the datasheet

That may fix it right there, if not try adding a brief kickstart to the code, set it to 255 briefly and then down to whatever speed you're after.
 
Back