I have this circuit to control my doorbell but the voltage on the pin 4 is only outputting 1.2V rather than 3.3V Then microcontroller is not detecting the button push because it thinks the circuit is already low. Is there a reason the pin is only outputting 1.2V. I think if it was 3.3V my program would work as I ran other tests on shorter wires at the program worked. Or is there another way to get the button to register?
What is that small square under the yellow switch wires? Please post a real schematic, not a Fritzing.
I see a push button (doorbell switch?) with a 330ohm resistor in series, connected between pin and ground.
Did you use INPUT_PULLUP in pinMode?
Leo..
Hi,
Can you post your code please. so we can see how you read your button?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Not a fritzy image.
We need a schematic with labels on components and pins, for example the transistor and has been pointed out before, what is connected to the yellow wires? A press button ? ? ?
What is the 330R resistor for?
If you remove the press button, what voltage do you measure?
Thanks.. Tom.. ![]()
Ok I took a picture of a simple wiring diagram. I have since took the 300 ohm resistor out and it didn't seem to affect it. I was already using a pull up resistor. The voltage is staying at about 1.2V on pin D4. I am also posting my code below.
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Bounce2.h>
// Use these lines to setup MQTT Server and WiFi
#define MQTT_SERVER "redacted"
const char* ssid = "redacted";
const char* password = "redacted";
// Define GPIO on ESP8266
const int buttonPin = 2;
const int relayPin = 4 ;
// Topics to Publish or Subscribe to
char* doorbellbuttonTopic = "home/doorbell/doorbell";
char* doorbellsoundTopic = "home/doorbell/sound";
// Create an instance of the bounce class
Bounce doorbellbutton = Bounce();
// Define when relay is on or off
#define RELAY_ON 1
#define RELAY_OFF 0
// Define variables
unsigned int doorbellDelay = 5000; // Interval (milliseconds) at which to keep doorbell sensor triggered to prevent multiple ringings
unsigned int ringTime = 700; // How long the relay is on (milliseconds)
boolean doorbellSound = 1; // Used to keep track if the doorbell should sound or be silent. Value recieved from doorbell on/off switch (0=off, 1=on)
int buttonPush = 0;
int buttonState = 0;
struct bs_v {
boolean b = 0; // Value for button push
boolean s = 1; // Value for sound
};
struct bs_v bsvalues;
WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
void setup() {
// Initialize the button pin with a internal pull-up
pinMode(buttonPin, INPUT_PULLUP);
doorbellbutton.attach(buttonPin);
doorbellbutton.interval(5);
// Initialize the relay pin
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, RELAY_OFF);
// Start the serial line for debugging
Serial.begin(115200); // com to computer
delay(100);
// Start wifi subsystem
WiFi.begin(ssid, password);
// Attempt to connect to the WIFI network and then connect to the MQTT server
reconnect();
client.publish(doorbellsoundTopic, "1");
// Wait for a bit before starting main loop
delay(2000);
}
void loop() {
// Reconnect if connection is lost
if (!client.connected() && WiFi.status() == 3) { reconnect();}
// Maintain MQTT connection
client.loop();
// Monitor the button
checkButton();
// Handle doorbellbutton
if(bsvalues.b == 1 && bsvalues.s == 1){
digitalWrite(relayPin, RELAY_ON);
delay(ringTime);
bsvalues.b = 0;
digitalWrite(relayPin, RELAY_OFF);
delay(doorbellDelay);
doorbellbutton.update();
buttonState = doorbellbutton.read();
//Serial.println("Will ring");
}
else if(bsvalues.b == 0){
digitalWrite(relayPin, RELAY_OFF);
}
else{
digitalWrite(relayPin, RELAY_OFF);
}
// Delay to allow ESP8266 WIFI functions to run
delay(10);
}
// MQTT callback function -(Use only if topics are being subscribed to)
void callback(char* topic, byte* payload, unsigned int length){
// Convert topic to string to make it easier to work with
String topicStr = topic;
//char byteToSend = 0;
// Handle doorbellbuttonTopic
if(topicStr.equals(doorbellbuttonTopic)){
if(payload[0] == '1'){
buttonPush = 1;
(bsvalues) = passButtonPush(bsvalues, buttonPush);
Serial.println(bsvalues.b);
}
else if (payload[0] == '0'){
buttonPush = 0;
(bsvalues) = passButtonPush(bsvalues, buttonPush);
Serial.println(bsvalues.b);
}
}
// Handle doorbellsoundTopic
else if(topicStr.equals(doorbellsoundTopic)){
if(payload[0] == '1'){
doorbellSound = 1;
(bsvalues) = passDoorbellSound(bsvalues, doorbellSound);
Serial.println(bsvalues.s);
}
else if (payload[0] == '0'){
doorbellSound = 0;
(bsvalues) = passDoorbellSound(bsvalues, doorbellSound);
Serial.println(bsvalues.s);
}
}
}
struct bs_v passButtonPush(struct bs_v, int buttonPush){
//struct bs_v bsvalues;
bsvalues.b = buttonPush;
return bsvalues;
}
struct bs_v passDoorbellSound(struct bs_v, int doorbellSound){
//struct bs_v bsvalues;
bsvalues.s = doorbellSound;
return bsvalues;
}
void checkButton(){
//buttonState = digitalRead(buttonPin);
doorbellbutton.update();
buttonState = doorbellbutton.read();
if(doorbellbutton.fell() == 1){
client.publish(doorbellbuttonTopic, "1");
}
else if(doorbellbutton.rose() == 1){
client.publish(doorbellbuttonTopic, "0");
}
else{
//client.publish(doorbellbuttonTopic, "0");
//buttonPush = 1;
}
}
void failMQTTcheckButton(){
buttonState = digitalRead(buttonPin);
//buttonState = doorbellbutton.read();
doorbellSound = 1;
if(buttonState == HIGH){
buttonPush = 1;
}
// Else if relay is on and button is pushed publish Off state to doorbellbuttonTopic
else if(buttonState == LOW){
buttonPush = 0;
}
}
void reconnect() {
//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){
//debug printing
Serial.print("Connecting to ");
Serial.println(ssid);
//loop while we wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print out some more debug once connected
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
failMQTTcheckButton();
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
//if connected, subscribe to the topic(s) we want to be notified about
if (client.connect("(char*) clientName.c_str()", "redacted", "redacted")) {
Serial.print("\tMQTT Connected");
client.subscribe(doorbellbuttonTopic);
client.subscribe(doorbellsoundTopic);
}
//otherwise print failed for debugging
else{ Serial.println("\tFailed."); abort();}
}
}
}
// Generate unique name from MAC addr
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5){
result += ':';
}
}
return result;
}
Don't see any photo......
It should be added now. It was too large the first time
Which transistor are you using? Are you using a plain relay or a relay board? For the plain replay, your circuit is missing a fly-back diode.
Note
Your schematic from reply #5 does not seem to bear any resemblance with the fritzing diagram from the opening post. The powerlines in the fritzing diagram seem to be directly connected to the relay so the relay is always activated. I might see that wrong though.
The relay is not shown in the fritzing image. The black box at the bottom converts AC voltage to DC 5V. The relay would be connected to the to the blue pins in the bottom right of the fritzing. I am using and NPN transistor 2n2222.
I have a relay module board so the diode is built in.
Should have seen that
Sorry.
Hi,
const int buttonPin = 2;
const int relayPin = 4 ;
I think you have a problem here.
Your buttonPin is 4
You relayPin is 2
Tom.... :o :o :o
Good spot, Tom. That should be corrected, but the other problem is that they should be D2 and D4 not just 2 and 4 in the code. If the "D" is not used, the number means the native pin number of the esp module, not the pin number printed next to the pin on the NodeMcu board.
I have a relay module board so the diode is built in.
Most relay modules have a transistor built in also. Maybe you don't need the 2n2222. Many relay modules won't trigger with 3.3V if they are powered by 5V. it would be worth trying to connect the 5V pin on the relay module to the 3.3V output on the NodeMcu. Then connect the relay input direct to the digital pin. It's not guaranteed to work, but many do.
@grangemd, I can see that you know how to draw a schematic. Fritzing can do this also, you can use the "schematic view" instead of the "breadboard view".
TomGeorge:
Hi,const int buttonPin = 2;
const int relayPin = 4 ;
I think you have a problem here. Your buttonPin is 4 You relayPin is 2 Tom.... :o :o :o
So D4 is GPIO 2 and D2 is GPIO 4 so I think the way this is set up is ok because I put it without the D here so it goes to the GPIO number.
Most relay modules have a transistor built in also. Maybe you don't need the 2n2222. Many relay modules won't trigger with 3.3V if they are powered by 5V. it would be worth trying to connect the 5V pin on the relay module to the 3.3V output on the NodeMcu. Then connect the relay input direct to the digital pin. It's not guaranteed to work, but many do.
So a little background on what happened. I had a an ESP8266 for this circuit which was working for awhile but as of a few weeks ago it stopped working for some reason. I thought maybe it had something to do with the doorbell so I tried replacing that but it still wasn't fixed so now I decided to change out the ESP2866 with a NodeMCU because it is much easier to reprogram. So I know the circuit works but now I am just trying to troubleshoot.
I have opened and MQTT subscribe feed and when I push the doorbell button there is no number sent to MQTT saying the button was pushed so I know that that is my problem. (hence why i thought maybe it was the button) I think it has to do with the pin voltage being low so when it goes to ground (when i push the button) it thinks it is already low thus doesn't signal a change.
So D4 is GPIO 2 and D2 is GPIO 4 so I think the way this is set up is ok because I put it without the D here so it goes to the GPIO number.
Correct. Apologies.
So, your OP said that the pin was outputting only 1.2V. Have you checked the voltage from the pin if the circuit is disconnected from it? It should read 3.3V. There is a 12K pull-up on that pin on the NodeMcu board. In theory, you do not need INPUT_PULLUP as well, but it won't cause a problem.
Have you checked the cable & door switch, in isolation, for continuity/short circuit? What is the resistance when the button is pushed and not pushed?
Ok before I read your post this is what I found out. The button seems to be pulling the pin to ground even when the button is not pushed. I unhooked the wire and jsut touched the wires together and it rings and when I hook the button back up it rings once. I am not sure why this happens. I am guessing maybe it is the light in the button. Maybe if I buy one that is not lighted the voltage will stay at 3.3 V but just my thoughts for now.
The button has a light in it? This is exactly the kind of thing you should have told us in your first post. Neither of your schematics mentioned this. Now we are on page 2 of the thread and we find out you have been concealing important details and as a result, wasting our time.
Well I am sorry. The first switch I had didn't have a light on it but the new one did. I put the old one back on at it seems to work. Sorry for troubling you
Not the answer but ...For this sort of task I would use an external pull up resistor of say 1kohm - the internal pull up is a high value and you can get problems with long lead length to your switch .
Try running the digital input example and get that working

