Asking help for button

I need to change this A S1 button into 6x6mm small button as shown in photo. All the codes are written and worked well with A S1 button. The problem is I cannot find any tutorials for using 6x6 mm button, and literally don't know how to solder and connect with wires. I asked AI and it said solder it diagonally, but A S1 has three pins (Pin Signal, 5V and GND) but 6x6 mm only has two, so how can it be used? If there is some tutorials or previous projects that I can see, can you share it please? Thanks a lot for help.


Hello, can you please share your code using the < code > tags.

The top button seems to have the contact pads under those screw holes, and without any DuPont connectors like the bottom image has, I can see how that would be difficult to wire.

Also to clarify the bottom image further, the reason it has the 3 pins is because it has what is known as a Pull-up Resistor on it, the little black rectangle with numbers just below the button.

It takes in 5v / gnd and if the button is not pressed, then it outputs 5v or a HIGH signal or a low signal when pressed.

You said you don’t know how to solder? This complicates things a bit… Do you know how to strip a wire to expose the copper inside? If so, then you could just wrap the copper through and around the screw holes. One for the signal and other for going to gnd (ground). Then in your code, you would need to set that specific pin from INPUT to INPUT_PULLUP. This will give you the same functionality you had before with the bottom image.

// 定义两个霍尔传感器引脚
const int HALL_PIN_1 = 3;  // 第一个传感器(上面)
const int HALL_PIN_2 = 4;  // 第二个传感器(下面)

// 定义按钮引脚
const int BUTTON_PIN = 5;   // 按钮引脚

// 存储状态
int state1 = 0;
int state2 = 0;

// 按钮相关变量
int buttonPressFlag = 0;     // 按钮按下标志
int displayMode = 0;         // 显示模式:0=显示最后停留位置,1=显示全图
unsigned long lastButtonPressTime = 0;  // 上次按钮按下时间
unsigned long buttonDebounceDelay = 200; // 按钮防抖延迟(毫秒)

// 记录最后检测到的位置
String lastPosition = "上面";  // 记录最后停留的位置(上面/下面)

void setup() {
  Serial.begin(9600);       // 串口波特率9600
  
  // 设置传感器为输入模式
  pinMode(HALL_PIN_1, INPUT);
  pinMode(HALL_PIN_2, INPUT);
  
  // 设置按钮为输入上拉模式(按下时读取为LOW,松开为HIGH)
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  Serial.println("===== 双路 HW-477 传感器 + 按钮控制测试 =====");
  Serial.println("无磁铁 = 高电平 | 有磁铁 = 低电平");
  Serial.println("-------------------------------------------");
  Serial.println("【按钮功能】");
  Serial.println("  按一下 → 切换显示模式");
  Serial.println("  当前模式:显示最后停留位置");
  Serial.println("===========================================\n");
}

void loop() {
  // ========== 1. 读取霍尔传感器状态 ==========
  state1 = digitalRead(HALL_PIN_1);
  state2 = digitalRead(HALL_PIN_2);
  
  // 更新最后停留位置(只有当传感器检测到时才更新)
  if (state1 == LOW) {
    lastPosition = "上面";
  }
  if (state2 == LOW) {
    lastPosition = "下面";
  }
  
  // ========== 2. 读取按钮状态并处理切换(简化版)==========
  // 检测按钮是否被按下(在INPUT_PULLUP模式下,按下为LOW)
  if (digitalRead(BUTTON_PIN) == LOW) {
    // 检查距离上次按下是否超过了防抖时间
    if (millis() - lastButtonPressTime > buttonDebounceDelay) {
      lastButtonPressTime = millis();  // 记录按下时间
      
      // 切换显示模式
      if (displayMode == 0) {
        displayMode = 1;
        Serial.println("\n🔘 按钮按下!切换到【显示全图】模式 🔘");
      } else {
        displayMode = 0;
        Serial.print("\n🔘 按钮按下!切换到【显示最后停留位置】模式 🔘");
        Serial.print(" 当前位置:");
        Serial.println(lastPosition);
      }
      
      // 等待按钮松开,避免重复触发
      while(digitalRead(BUTTON_PIN) == LOW) {
        delay(10);  // 等待按钮松开
      }
    }
  }
  
  // ========== 3. 根据当前显示模式执行相应的动作 ==========
  executeDisplayAction();
  
  // ========== 4. 串口输出传感器状态和当前模式 ==========
  printSensorStatus();
  
  delay(200);  // 降低刷新频率,避免串口刷屏太快
}

// 根据显示模式执行具体动作
void executeDisplayAction() {
  if (displayMode == 1) {
    // 模式1:显示全部图像
    // 在这里添加显示全图的代码
    // 例如:drawFullImage();
  } else {
    // 模式0:根据最后位置显示上面或下面
    if (lastPosition == "上面") {
      // 在这里添加显示上面的代码
      // 例如:drawTopPart();
    } else if (lastPosition == "下面") {
      // 在这里添加显示下面的代码
      // 例如:drawBottomPart();
    }
  }
}

// 串口打印传感器状态和当前模式
void printSensorStatus() {
  // 显示传感器1状态
  Serial.print("传感器1: ");
  if (state1 == LOW) {
    Serial.print("✅ 上面 ");
  } else {
    Serial.print("❌ 未检测到    ");
  }

  // 显示传感器2状态
  Serial.print("| 传感器2: ");
  if (state2 == LOW) {
    Serial.print("✅ 下面");
  } else {
    Serial.print("❌ 未检测到");
  }
  
  // 显示当前显示模式
  Serial.print("  | 模式: ");
  if (displayMode == 1) {
    Serial.print("【显示全图】");
  } else {
    Serial.print("【显示位置: ");
    Serial.print(lastPosition);
    Serial.print("】");
  }
  
  // 显示最后停留位置
  Serial.print("  | 最后停留: ");
  Serial.print(lastPosition);
  
  Serial.println();
}

Your original switch (bottom photo) appears to be a KY-004 module. The switch is wired between the - and the S terminal and is closed when pressed. It also has a 10k resistor onboard, which according to information online appears to be wired like this with the resistor between the positive and negative terminals:

In this configuration, the resistor would be neither a pull-up nor a pull-down, so I am not sure what its purpose would be? The switch just appears to connect the signal side to ground when pressed.

According to its description, the switch is meant to "send a positive signal when pressed"?

If the ground were not connected then it would connect +V to the signal pin via the 10k resistor. If signal and ground were reversed then it would act as a pull-up. However, as per how the pins are labelled, I am not sure what its meant to do? Initially I thought the diagram must be wrong, but then found a couple of videos where someone checks it with a DMM and confirms that they are wired in this way.

Your replacement module seems to be a simple switch mounted on a board and without the resistor. If you look along the long edges you will see a track that runs from one end to the other through a pair of switch pins with a solder pad at each end. The switch when pressed will connect one long side to the other long side. You can just use one set of pads at either of the short edges. These will correspond to - (ground) and S. If you also need the VCC connection, then a 10k resistor might need to be added.

It would help to see how your current KY-004 switch is wired and your code to understand how it is being used. That would help us to advise how the replacement needs to be connected.

Obviously the soldering aspect needs to be dealt with as well. Its not that difficult and there are a number of tutorials online if you search "soldering basics". It might take a couple of attempts to get right so it helps if you have something to practice on first. On the other hand, for the sake of soldering just a couple of wires, it might be easier to get a friend who already has the skills to do this for you.

UPDATE: Our posts crossed and I Just noticed that you posted your code.

Your code is setting BUTTON_PIN to INPUT_PULLUP and then checks for LOW to see if the button was pressed. That makes perfect sense. The internal pull-up is used to keep the pin in a known state (HIGH) when it is not pressed. All you need to do is connect one side of the switch to ground and the other to BUTTON_PIN pin and it should work the same.

Thanks for replying. I don't know which pins to connect, 5V and GND or Pin and GND? That's what I was confusing about.

You need to solder wires here:

Pin and gnd

Got it. Thanks a lot!!!

Thanks for everyone whoever replying and helping me. It solved. I soldered it diagonally (I know it's a bit ugly), one connecting to the Pin and the other to the GND. It worked the same way as A S1 button, without having any problems. From here, thanks again to everyone helping me.

Thanks. I solved it like this and it works well.

As long as the connections are on opposite sides of the board, it will work.
Have fun

Is the above Ground is the "Signal Common Point(equipotetiality)?" If yes, then its standard symbol is:
image

That makes no sense.

I have just changed the symbol and NOT the schematic.

For what purposes the following symbols are used:
image
image
image

In many industrial electronic systems, the signal common (reference) is kept isolated from the power ground (GND) to reduce noise and improve signal integrity. In some designs, however, the signal common and ground may be connected to the chassis at a defined point, depending on the system’s requirements for grounding, shielding, and safety.

You replaced the symbol for a pin connection with a triangle.
Makes no sense.

Is the signal common (return/reference) connected to the power ground? If so, the signal input (injection point) and the signal return should not be represented using identical symbols, as they serve different roles in the circuit.

This is not a triangle but a standard symbol for signal reference.

If you want to refer that "pin connection point" as a "test point", then this is fine; and, there are indeed test points (TP) on PCB.

That makes even less sense.
The original schematic is correct.

@jim-p , thanks for the correction. On your enlarged photo it is much clearer that the line between contacts is just a line on the silk screen. I thought the track ran straight through. So yes, opposite ends is correct. OP sorry if my comment confused.

BTW, can you make sense of the KY-004 button circuit?

Even, I have seen like below:

COMM is the return/ref point for Signal.
GND is the return/ref point for +Vin.
TP is the test point to connect DV/Scope ground terminal.
chassis is the body/earth potential of metallic enclosure.

Where?