Pin going high when set to low

Hello. I'm currently using Tinkercad circuits, and I find when I set pin 2 to either High or Low, it sets that pin as high anyway. Using it for relays to control a filament extruder I'm building. Here's the code:

// C++ code
//
#include <Adafruit_LiquidCrystal.h>

int heaterThresh = 0;

int button_1 = 0;

int button_2 = 0;

int button_3 = 0;

Adafruit_LiquidCrystal lcd_1(0);

int counter;

void setup()
{
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  lcd_1.begin(16, 2);
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(A0, INPUT);

  button_1 = analogRead(A1);
  button_2 = analogRead(A2);
  button_3 = analogRead(A3);
  button_1 += 1;
  button_2 += 5;
  button_3 += 5;
  lcd_1.setCursor(0, 0);
  lcd_1.print("Arduino Extruder");
  lcd_1.setCursor(0, 1);
  lcd_1.print("            V1.0");
  delay(2500); // Wait for 2500 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.scrollDisplayLeft();
  delay(90); // Wait for 90 millisecond(s)
  lcd_1.clear();
  delay(750); // Wait for 750 millisecond(s)
}

void loop()
{
  lcd_1.setCursor(3, 0);
  lcd_1.print("Select Mode");
  lcd_1.setCursor(0, 1);
  lcd_1.print("Start");
  lcd_1.setCursor(7, 1);
  lcd_1.print("Settings");
  Serial.print(button_2);
  if (button_2 < 1) {
  }
  if (analogRead(A1) >= 1018) {
    lcd_1.clear();
    lcd_1.print("15");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("14");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("13");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("12");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("11");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("10");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.clear();
    lcd_1.print("9");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("8");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("7");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("6");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("5");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("4");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("3");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("2");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("1");
    delay(1000); // Wait for 1000 millisecond(s)
    lcd_1.setCursor(0, 0);
    lcd_1.print("Starting...");
    digitalWrite(2, HIGH);
    for (counter = 0; counter < 50000; ++counter) {
      lcd_1.setCursor(0, 0);
      lcd_1.print((-40 + 0.878679 * (analogRead(A0) - 20)));
      delay(450); // Wait for 450 millisecond(s)
      lcd_1.clear();
    }
    delay(50000); // Wait for 50000 millisecond(s)
  }
}

Why do you show this code?
It has nothing to do with your question.

You don't set pin2 neither HIGH nor LOW in the code.
And how can you know that you have pin 2 HIGH (or LOW) if you do not check its level anywhere in the code?

Show circuit

Which board?

To my knowledge TinkerCAD is a simulator, In which case it might well be a bug in TinkerCAD. Build the real thing and test.

Some comments on your code (not fixing it).

You are aware that you can use analog inputs as digital inputs? So you can use digitalRead(A1).

Repeating code calls for looping; in it's simplest form e.g. a for-loop(). E.g. in setup() you use 16 times

That can easily be replaced by

  // scroll left 16 times
  for (uint8_t cnt = 0; cnt < 16; cnt++)
   {
    lcd_1.scrollDisplayLeft();
    delay(90);  // Wait for 90 millisecond(s)
  }

In loop you have 15 times this with de4crementing numbers

You can easily replace that with a for-loop.

    // countdown
    for (uint8_t cnt = 15; cnt > 0; cnt--) {
      lcd_1.print(cnt);
      delay(1000);  // Wait for 1000 millisecond(s)
      lcd_1.setCursor(0, 0);
    }

For now that will do. You should actually not use for-loops in combination with delay() but that is another story.

You only set the pin HIGH here, not low!

Do you have a pull-down on the gate of the MOSFET?

Wow. Pretty much my whole code was wrong wasn't it....

In which case you are correct. I tried Wokwi simulator and the code was working wonders....
P.S. Sorry I haven't posted in awhile, I forgot to put answer...

Take the chance to name your I/o with names that reflect what they are - impossible to follow any code which just uses 1,2,3,etc , use “ int heaterRelay” or similar

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