Code only works reliably when icsp connected to nano

Hi all,

I'm trying to make a temperature controlled 12V fan.

I have a 19.5v laptop power supply wired to 2 dcdc buck converters. Buck converters set to output 12v and 5v.
irf520n mosfet connected to pin 3 to allow pwm control. Ntc 3950 thermistor (100k) for temperature sensing.

I am programming a nano clone using the icsp header and an arduino uno as ISP. I have used the uno bootlader on the nano - seemingly with no difference.

The Main code below seems works as it is. However, if I use the mosfet control code that is currently commented out, it only works when the nano iscp is connected to the uno programmer. When iscp is not connected the fan just goes to the highest pwm value that is set.

The commented out mosfet control code works when uploaded to the uno and piggybacked to the correct sockets on perfboard setup - no nano connected.

Excerpt mosfet control code:

  if ( cputemp > 25 /*&& cputemp < 30*/ )
    analogWrite (MFA1, 20);
  else
    analogWrite (MFA1, 0);



  /*
    if ( cputemp > 35)
    analogWrite (MFA1,255);
    //else if ( cputemp > 30 && cputemp < 35 )
    //analogWrite (MFA1,100);
    else if ( cputemp > 25 && cputemp < 30 )
    analogWrite (MFA1,20);
    else
    analogWrite (MFA1,0);
  */

I feel I'm missing something obvious. Using the commented out code it works standalone with the uno, no issues. Exactly the same setup not playing ball with the nano. I have a feeling the temperature reading is somehow going wonky, but not sure how to reliably check it in situ with the nano setup.

Due to the way I'm programming the nano I can't see serial output.

Questions:
Why should the code behave differently on the nano vs uno?
Why does having the programmer connected "make it work"?
Can I wire the nanos serial to go through the uno to pc?

If anyone's got a direction to point me in would be really helpful. Let me know if you need more info.

Main Code:

int cputemp; //temperature reading

void setup() {
  Serial.begin(115200);

  TCCR2B = TCCR2B & B11111000 | 0x01;    // set timer 2 divisor to     1 for PWM frequency of 31372.55 Hz

}

void loop() {

  Temp1();

  int MFA1 = 3; //mosfet A1

  int maxspeed = 255;
  int minspeed = 10;
  int pwmoff = 0;

  if ( cputemp > 25 /*&& cputemp < 30*/ )
    analogWrite (MFA1, 20);
  else
    analogWrite (MFA1, 0);



  /*
    if ( cputemp > 35)
    analogWrite (MFA1,255);
    //else if ( cputemp > 30 && cputemp < 35 )
    //analogWrite (MFA1,100);
    else if ( cputemp > 25 && cputemp < 30 )
    analogWrite (MFA1,20);
    else
    analogWrite (MFA1,0);
  */

  delay (500);

}

//function for temperature sensing
bool Temp1() {

  int ThermistorPin1 = A0;
  int Vo;
  float R1 = 10000;
  float logR2, R2, T;
  float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

  Vo = analogRead(ThermistorPin1);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  T = T - 273.15;
  // T = (T * 9.0)/ 5.0 + 32.0; //Disabled for Celsius

  Serial.println(T);
  cputemp = T;

}

schematic

Picture (I'm sorry if my soldering gives you brainache - it functions don't stare too long :). Resistor is hidden under the nano.

Make sure that your code is formatted before posting!.

Mark

Mutton44:
Hi all,

I am programming a nano clone using the icsp header and an arduino uno as ISP. I have used the uno bootlader on the nano - seemingly with no difference.

Hi,
Welcome to the forum and thank you for reading the "how to" post on using the forum.

Why aren't you using the USB port on the Nano?
Can I suggest you use the Nano USB and add some Serial.prints to help debug through the IDE monitor.

Do you have a DMM to measure some circuit parameters.

Also the MOSFET module you are using is NOT logic level and will not act as an efficient PWM switch.

You need to check how to format your if statements.

In the IDE, select TOOLS and then AutoFormat, it will auto indent your code to make it easier to read.

Tom.. :slight_smile:

Hi,
You will also need to

pinMode(MFA1,OUTPUT);
in void setup()

and declare

int MFA1 =3;

before the void setup()

Tom.. :slight_smile:

Many thanks for replies. I struggle with code :). Main post now with updated formatting.

TomGeorge:
Why aren't you using the USB port on the Nano?
Can I suggest you use the Nano USB and add some Serial.prints to help debug through the IDE monitor.

Windows10 device manager is seeing my nano clones as "Unkown USB Device (Device Descriptor Request Failed)". After reading around I thought I'd try a different approach. Ultimately I don't need the serial feedback. I didn't expect to encounter this issue though...

I'm not necessarily after the easiest way to do it, I don't mind learning a bit.

I do have a DMM.

TomGeorge:
Also the MOSFET module you are using is NOT logic level and will not act as an efficient PWM switch.

I have some P75NF75 which i was going to use but was I lazy and this one was mounted with convenient terminals :).

Efficiency aside I don't see why the uno works and the nano not?
Equally why the nano only works with the programmer connected. I'm baffled.
My understanding is that for my purposes they are basically interchangable. I'm happy to be put right on this though.

Cheers.

Hi,
Have you checked the voltage on the 5V pins of the Nano when the fault occurs.
Also the voltage on the analog input pin to check your thermistor circuit.

You really need Serial.print, to check if your thermistor is working and if the conversion code is working.
If the UNO works with the code, but the Nano doesn't, then you may have a wiring problem.

Tom... :slight_smile:

I've added the pinMode and moved the pin declaration.

3.9V on the 5V.
2.4-2.8V on analog dependant on temperature.
For good measure 5V on Vin.

Yeah, the serial print would be the most useful thing here... I think a geniune nano may be needed for bench debugging. I can't seem to get windows to play ball with the nanos, I have the CH340SER drivers but no joy as yet. I'll have another crack at that too.

I can't find a fault with the wiring. I suppose it's not to say there isn't :). but after buzzing through with the meter, no shorts, everything upto the sockets and from them is okay. For a final test of the wiring I used breadboard wires from nano sockets (no nano inserted :slight_smile: ) to the corresponding pins on uno with the same sketch. It should have then been a case of shrinking the uno and, bam, there we are. Apparently not...

And back i am at the serial monitor :).

edit: If i unplug the thermistor the fan stops. I think it's reading slightly high as fan goes to the maximum pwm speed and dosen't like going off. Hence my simpler on and off code at 25 degrees. I can reliably heat something to around 37 at my bench :). Trial and error could be a good game... or not.

Mutton44:
I've added the pinMode and moved the pin declaration.

3.9V on the 5V.
2.4-2.8V on analog dependant on temperature.
For good measure 5V on Vin.

So you have the 5V from the DC-DC connected to Vin, not 5V as in your circuit diagram?

Connect the 5V from the DC-DC to the 5V pin on the Nano.

The Vin pin is the input to the onboard 5V linear regualator, it needs 8V or more to give a 5V regulated supply to the 5V pin and the control circuitry.

You are only supplying 5V to Vin, so no regulation, that is why you are measuring 3.9V on the 5V pin.

Tom... :slight_smile:

Hi Tom, many thanks for your time on this.

TomGeorge:
So you have the 5V from the DC-DC connected to Vin, not 5V as in your circuit diagram?

Apparently, yes. Missed that for looking. Thankyou!

I have now altered the wiring.

The serial monitor is now working over usb too.
Re-installed Arduino IDE.
After connecting nano to pc again
Uninstalled drivers for nano’ COM port (in device manager)
Reinstalled drivers again(windows has worked itself out here).

I can’t program it over USB but I can see the serial output. Programming now involves switching between ICSP and USB, bit awkward but hey, overall it works.

With the serial now somewhat working I could see the temperature sensor reading. Around 80°C. This has turned out to be a connector that seems connected, but isn’t quite. Difficult to visually spot. This explains the apparent over-reading and subsequent ramp to max speed.

So now everything is functioning as expected with the following code (and wired to the schematic :slight_smile: ):

int cputemp; //temperature reading
int MFA1 = 3; //mosfet A1

void setup() {
  Serial.begin(115200);

  TCCR2B = TCCR2B & B11111000 | 0x01;    // set timer 2 divisor to     1 for PWM frequency of 31372.55 Hz

  pinMode(MFA1, OUTPUT);

}

void loop() {

  Temp1();

  int maxspeed = 255;
  int minspeed = 10;
  int pwmoff = 0;

  if ( cputemp >= 35)
    analogWrite (MFA1, 255);
  else if ( cputemp >= 30 && cputemp < 35 )
    analogWrite (MFA1, 100);
  else if ( cputemp >= 25 && cputemp < 30 )
    analogWrite (MFA1, 20);
  else
    analogWrite (MFA1, 0);

  delay (500);

}

//function for temperature sensing
bool Temp1() {
  int ThermistorPin1 = A0;
  int Vo;
  float R1 = 11000;
  float logR2, R2, T;
  float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

  Vo = analogRead(ThermistorPin1);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  T = T - 273.15;
  // T = (T * 9.0)/ 5.0 + 32.0; //Disabled for Celsius

  Serial.println(T);
  cputemp = T;

}

The only other thing I have been struggling with is removing the global variable “cputemp”. Is this possible. I’m struggling with returning numbers from functions. They elude me somewhat.

Am I on the right track by declaring this for the function?

bool temp1 (float T);

If that’s a no, I think it’s back to reading :).