2 Button to 1 PWM output

I'm trying to control my ventilation with heat recovery unit, it needs a signal from 1 - 10 volt

10 volt == Low ventilation
5 volt == Middle ventilation
0 volt == High ventilation

Normally i want low ventilation, when the recovery unit starts i will take that signal to set the arduino to 5 volts. Beceause it needs more ventilation, else the unit will freeze. And then I want to use a push button when i start to shower (With timer).

I'm using the pwm signal to control a mosfet module IRF520. I know it's not a logic mosfet but it is ussable. I made a code in tinkercad, and there it works. But in real life it doesn't work at all. I just don't get it.

This is the code:
const int BUTTON1 = 3;
const int BUTTON2 = 1;
const int LED = 11;

int BUTTON1state = 0;
int BUTTON2state = 0;

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {

BUTTON1state = digitalRead(BUTTON1);
BUTTON2state = digitalRead(BUTTON2);

while (BUTTON1state == 1 && BUTTON2state == 1)
{
analogWrite(11, 0);
delay(60000);
break;
}

while (BUTTON1state == 0 && BUTTON2state == 1)
{
analogWrite(11, 0);
delay(60000);

break;

}

if (BUTTON1state == 1 )
{ analogWrite(11, 125);
}
else {
if (BUTTON1state == 0 )
analogWrite(11, 255);

}
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

What does that mean ?

I'm getting no output. from the mosfet. I can manual switch the mosfet with a high pwm signal then it works (12v) also with half level pwm I get 5 volts.

Use a logic level N channel MOSFET.

Hello
Post a real schematic of your project.

Look at the following diagram (Fig-1) and observe that the anaologWrite() function works on the described DPins execpt Dpin-8.

pwm328x
Figure-1:

I'm only looking at output pin 11 for the pwm signal.

There are only 2 buttons, it is no rocket science.

I only need a pwm singal on pin outlet 11

I'm really sorry that I don't post everything like is has to be

so when the switch is low i need 5v pwm
when the switch is high i need 2.5v pwm
if the button is pushed i always need 0v pwm (with timer).

Im just frustrated that it won't work.

Hello
That isn´t a real schematic.
Use a piece of paper and a pen.

1. You have one switch. Correct?
2. At what DPin (digital pin) is the switch connected?
3. Switch is low -- does it mean the switch is closed?
4. Switch is high -- does it mean the switch is opened?
5. Button is pushed -- does it mean that you have another switch named Button? It is pushed -- does it mean that the Button is closed?

See image.

I´m out.
Have a nice day and enjoy coding in C++.

Both 3 and 1 on this diagram are permanently low regardless of button states

Try and mount button rotated 90 degrees...

1. I recommend you to follow the diagram of Fig-1; where,Button is connected at DPin-2 and not DPin-1 as it is engaged with Serial Monitor/PC/IDE.

sw2Led1
=Figure-1:

2. Upload the following sktch and report the result.

#define Switch 3
#define Button 2
#define PWM 11

void setup() 
{
  Serial.begin(9600);
  pinMode(Switch, INPUT);
  pinMode(Button, INPUT);
}

void loop() 
{
  if(digitalRead(Switch) == LOW) //Switch is opened
  {
    analogWrite(PWM, 255); //100% duty cycle ~= 5V
  }
  
  if(digitalRead(Switch) == HIGH)  //Swicth is closed
  {
    analogWrite(PWM, 128);  //50% dut cycle ~=2/5V
  }
  
  if(digitalRead(Button) == HIGH) //Button is pushed
  {
    analogWrite(PWM, 0);   //0% duty cycle ~=0V
  }
}

Thanks guys for helping, I changed the ports for button 1 to 6 en button 2 to 5. And now it works perfect!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.