PWMOUT and READTEMP need HELP!

#1
hey guys,

im in need of some help. i have a code here that i found in another thread but its not working like it should iv tried my best to get it to work and got nothing.

Code:
#Picaxe 14M2
#no_data
setfreq m4

symbol fan      = B.2    
symbol temp  = B.1   

symbol temperature = B0
symbol duty = W1

Inti: 
pwmout fan,39,0    'Enable PWM Freq = 25KHz /  Duty = 0  

Main:      'Main Loop 
'-------------------------------------------
	pwmout fan,39,159
	pause 2000 '1.25sec
	pwmout fan,39,0
   do      
      
      gosub Temp_Loop
      pause 1000   
   
   loop
'---------- END MAIN ------------------------


                
Temp_Loop:    'Sub Routine       
'--------------------------------------------------    
    readtemp temp,temperature       
    select case temperature         
              
       case =< 25        :  duty = 79     
        case  25 to 29  :  duty = 95           
        case  30 to 34  :  duty = 111            
        case  35 to 39  :  duty = 127   
	  case  40 to 45  :  duty = 143  
       case  > 45      :  duty = 159        
                     
    endselect
    
    pwmduty fan,duty     'Change fan speed according to temp 

return 'to main
'---------------- End SUB --------------------------
please help me fix it

cheers Jordan
 
Last edited by a moderator:

premelec

Senior Member
#2
Oh Jordan you tease! WHAT doesn't work? What have you got connected? Picture , schematic etc... my thing doesn't work either :)
 
#3
everything is connected on a bread board (tried to upload image but not working) the thing that does work is the fan speed doesnt change even know iv added heat to the temp sensor
 
#5
Perhaps try code that increments up the PWMDUTY if the temperature goes up, and vice versa. The select. . .case you use in the your code doesn't give you much range for PWMDUTY.
 
#7
This code is assuming 25 C is the temperature you want to maintain

Code:
readtemp temp,temperature

 
if temperature > 25 then
duty = duty + 1 max 1023
endif
if temperature < 25 then
duty = duty - 1 min 0
endif

pwmduty fan,duty 'Change fan speed according to temp 

return 'to main
 

hippy

Technical Support
Staff member
#9
I would first check that the fan speed can be controlled by varying the duty setting.

If so the code in post #1 would be expected to work. Check that the temp sensor and motor are connected to the correct pins, and add a DEBUG or SERTXD to show what the temperature read and duty being set are, see if the fan speed is being instructed to change.
 

Goeytex

Senior Member
#10
I am looking at the photo of the breadboard and trying to make sense of how the fan is being driven. I see a small transistor and what may or may not be a FET. What I don't see are any resistors. No resistor to the base of the small transistor?

Perhaps you could take a photo from directly above the breadboard so that we can actually see how things are wired up. Also a schematic that shows how the transistor/ /Fet are connected would be quite helpful.
 
#11
There are no resistors visible in the photo &#8211; also rather hard to visually trace the wires in general.
I assume that the TO-92 part is the temp sensor ???? :confused:

The DS18B20 requires a resistor, typically 4.7 kOhm between the PICAXE IO pin and the +5V line.

See PICAXE Manual 2 bottom of page 185 : http://www.picaxe.com/docs/picaxe_manual2.pdf

Without the pull-up resistor both the DS18B20 and the PICAXE will only see zero bits and no data.
In fact with the PICAXE sending a temp conversion command and then polling for an answer it may be locked on the READTEMP command.
 
#12
The code is fine but the line above will not do what you expect.
If duty is 0, then duty - 1 will become 65535 and the min 0 will do nothing.
It needs to be rearanged as "duty = duty min 1 - 1"
Or, more generally "duty = duty min N - N"
Thank you for pointing that out. Might cause strange things to happen if not fixed.
 
#13
the fan works with the duty cycle i checked it before adding the temp sensor and yes ur correct i have no resistors on the board at all. the fan is driven by a MOSFET because its a 5V fan. and i will add a resistor to the temp sensor and see if that fixes the problem!
 
#17
ohhh ok, that would make sense and no i haven't added that yet i will be adding that today and making the schematic today as well. i am running the project from 4 AA batteries 1.2V each

Edit:
what size decoupling capacitor should I use ?
 
Last edited:
#19
That you are now reading some data albeit the power-on-reset value is better.

Is the temperature sensor definitely a DS18B20 (aka DS18B20+) and not a DS18B20-PAR as they are two different parts.

The DS18B20 / DS18B20+ and the DS18N20-PAR all use the same commands for temp conversion, scratchpad operations.
The difference being that the DS18B20-PAR only operates in parasitically powered mode only.
The DS18B20 can operate in both separately powered and parasitically powered moded.

However the PICAXE READTEMP command is only intended for use with separately powered DS18B20/DS18B20+ chips.

As I previously mentioned, 85 &#8304;C is the default value as a &#8216;Power On Reset value&#8217;.
If the DS18B20 was not receiving the &#8220;skipROM&#8221; command (%1100 1100) and &#8220;Tconvert&#8221; command (%0100 0100) due to a poor PICAXE to DS18B20 data line connection then unlikely that the PICAXE would read back the POR data for 85 &#8304;C.
The 1-Wire data for 85 &#8304;C is %0000 0101 0101 0000 and keeping in mind that 1&#8217;s and 0&#8217;s are in fact not highs and lows but pulses of different durations it is seemingly unlikely to result from a poor connection for ground or signal between the DS18B20 and PICAXE, however a poor power supply connection making the DS18B20 operate in parasitic mode and possibly resetting due to voltage dip (below 3.3V) while drawing a higher current in the Temp conversion period.
 
#21
Please post your circuit schematic diagram and if possible a clear photo showing the wiring so we can trace through and ensure all is correct.
What are the markings on the DS18B20 that you are using?
 
#28
ok well everything works with the fan however its the 18b20 i'm having trouble with, its not reading the right values, it keeps reading only one value "85"
 
#29
Try disconnecting the FET and fan.

Your code from post #25 and your circuit from post #22 work for me on hardware. I don't have that FET or the fan, but it might be some interference from the motor upsetting things.

If it doesn't work with these disconnected there must be a fault in your wiring or one of your components.

If you are using PE6 there is a button on the Home menu to copy your code to the forum in color.
 
#30
What is your power source - plug-pack/wall-wart or AA batteries?

There could be voltage dips resetting the Ds18B20 hence the returned value of 85.
Try adding an electrolytic capacitor say 200uF to 470 uF and see if that helps.

Using a multimeter test to check you have a connection from the power source to the supply pin on the Ds18B20.

The fact that you read the 85 shows the signal is okay so ground and data lines to the Ds18B20 should be okay.

Even try moving the DS18B20 on the breadboard if nothing else suggested by Sig or I works.
Occasionally breadboard can have a bad connection at some holes.
 
#32
I think that we are starting to run out of ideas / suggestions for this one.

Others have tried you code for the DS18B20 and it works.
You can read the value 85 from the DS18B20 &#8211; so it is not entirely &#8220;dead&#8221;.
That you can read the value 85 means the PICAXE IO pin is working.

Do you have another DS18B20 you can try?
 

Goeytex

Senior Member
#33
I still suspect a wiring problem. Also there is no series resistor from the Picaxe to the Mosfet Gate. The photo shows messy wiring and improper use of the breadboard. The power rails are meant to be used. I am not nit-picking here, just trying to get the sensor working. This should not take 5 pages of posts. Below is a screen shot of how I would do it on a breadboard, no messy wiring and power rails used as intended. Capacitors used where needed. Resistors in their proper place. As a student you should be able to present a breadboard that you are proud of and not some bowl of spaghetti looking mess.

Messy breadboards are reserved for grey-beards and seasoned engineers. :)

Redo your breadboard and make it look like the ones below. And I ask the others to check my connections to make sure I got them correct as I am still have a bit of trouble seeing and thinking clearly.

Hippy's idea is a very good troubleshooting technique. It eliminates the motor and motor circuitry as a potential source of noise, or power supply droop that could affect the sensor.

So configure "No Motor" breadboard below first. Do not take shortcuts, or omit any parts. Then program with a simple program that just reads the sensor and display the data via sertxd. Let us know the results.
No_Motor.JPG


If the sensor is displaying valid data , then configure the breadboard according to the "with motor layout" below. Again, do not omit any parts. And use the values shown. Note that I moved the PWM to C.2, so change your code accordingly.

I also added a 470R series resistor between C.2 and the Mosfet Gate to prevent excessive peak current when the Mosfet is switching. The 47K resistor is to prevent the Mosfet from turning on at power up when the C.2 pin would otherwise be floating.

With_Motor.JPG

Let us know how it works out.
 
Last edited:

hippy

Technical Support
Staff member
#35
I would suggest a circuit which doesn't have anything except download circuit and DS18B20; forget the PWM and fan control for now, leave those parts off the breadboard, focus on getting the DS18B20 working. Use test code which just reads the DS18B20 serial number and temperature and reports those via SERTXD.
 
Top