Hi everyone,i'm a complete newbie to arduino(using arduino MEGA) and i have been given a task of creating a circuit where a relay is connected to power outlet and a switch button as well,whenever the button is pressed the relay should switch as well. I was able to code for the button to work when clicked,so i could see it on the serial monitor.Somebody please help me find a solution as i need to get this done by the weekend ![]()
Please read the advice in the topic "How to get the best out of this forum."
From Your post You canget more guesses then You have time to read.
Calm down and post what You've done, like code and wiring. Pen and paper is fine. Power sources are important, datasheet for the relay...
-
Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components. -
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
Post that code. What is the relay's coil voltage and current (or wattage)?
This is the code i used to detect and display the output on serial monitor when the button is clicked:
#define BUTTON_PIN 13
void setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
Serial.println(digitalRead(BUTTON_PIN));
delay(100);
}
and this is the circuit used,now i need to add a relay switch to the project which gets activated when i click the button.
Hi guys,any solution?
but i need to connect an mp3 player as well,hence why using arduino
yeah my bad for that... i thought ijust need this relay switch to be wired and coded on arduino and then mp3 module i can program it. anyways could you please explain how i could do the wiring and code on arduino?
okay so basically i need to design a system where LED lights and a 5 second sound gets played when we click a button on a plinth,my plan is to connect a relay to LED and then mp3 module with the 5 second file on it to the arduino and make it work when we click the button..
please if anyone knows and can tell me how to wire the circuit and the code for it ,it would be helpful as i have a deadline by monday ![]()
Hi guys,i need help to create a system using arduino where when i click a button,lights nd audio file should play for 5-10 seconds and then return to normal. My idea was to use a relay and connect the bulb to it and the power source, use a DFPlayer mini with the audio file in it and program it in a way when i click the button the relay gets activated and then the music and light plays. i'm a complete beginner in this so i need your expert help in the coding and wire diagram for the project. I'm using a arduino mega,NC/NO Switch button,DFPlayer Mini,and a 12 V relay
Draw this idea.
Your first drawing does not need to be perfect. That will come through improving the drawing until it is perfect.
Read about the DFPlayerMini. You can connect it in a way where programming is not needed to play music. You can write a program for the Arduino Mega2560 to control the other devices. If you are using a 12VDC relay, you must have a 12VDC power supply.
Interesting project but we are not a free design or code writing service. We will be happy help out with your design and or code but first you have to make an attempt to design it, write it, post it and explain what is not working properly.
If there is hardware it is always best to post links to technical information as there are many versions of the same or different items. Since we cannot see your project, my eyes are to weak, you need to post using the language of electronics, an annotated schematic (best) or a clear picture of a drawing. Pictures never hurt. Frizzing diagrams are not considered schematics here.
Hi, this is the code i used for it: `#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
const int buttonPin = 2; // Pin connected to the button
const int relayPin = 3; // Pin connected to the relay
SoftwareSerial mySerial(18,19); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
bool buttonPressed = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
mySerial.begin(9600);
if (!myDFPlayer.begin(mySerial)) { // Use softwareSerial to communicate with MP3
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); // Set volume value (0~30)
// Set button pin as input
pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
// Set relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is initially off
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == LOW) {
if (!buttonPressed) {
buttonPressed = true;
// Activate the relay for 5 seconds
digitalWrite(relayPin, LOW);
delay(5000); // 5 seconds delay
digitalWrite(relayPin, HIGH);
// Play music for 5 seconds
myDFPlayer.play(0001); // Replace with your MP3 file number
delay(5000); // 5 seconds delay
// Stop music playback
myDFPlayer.stop();
buttonPressed = false;
}
} else {
buttonPressed = false;
}
}
the code compiles fine but relay does'nt switch nor does the dfplayer work
Arduino Mega DFPlayer Mini Relay Module Switch Button (NO)
_______________ ________________ ________________ ___________
| | | | | | | |
| | 5V | VCC GND | | VCC GND | | |
| |----------|----------------| |----------------| | |
| | GND | TX RX | | | | |
| |----------|----------------| | | | |
| | 1 | 0 | | 3 | | 2 |
| |__________|________________| |________________| |___________|
| | | |
| | | |
| | | |
GND GND GND GND
(Arduino GND) (12V Power Supply) (Relay GND) (Switch GND)
l am not sure what you posted and called code, it is gibberish to me. Read the forum guidelines and post using code tags. What is that thing with a lot of dashes and labels, it does not look like a schematic?
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
const int buttonPin = 2; // Pin connected to the button
const int relayPin = 3; // Pin connected to the relay
SoftwareSerial mySerial(18,19); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
bool buttonPressed = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
mySerial.begin(9600);
if (!myDFPlayer.begin(mySerial)) { // Use softwareSerial to communicate with MP3
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30); // Set volume value (0~30)
// Set button pin as input
pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor
// Set relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is initially off
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == LOW) {
if (!buttonPressed) {
buttonPressed = true;
// Activate the relay for 5 seconds
digitalWrite(relayPin, LOW);
delay(5000); // 5 seconds delay
digitalWrite(relayPin, HIGH);
// Play music for 5 seconds
myDFPlayer.play(0001); // Replace with your MP3 file number
delay(5000); // 5 seconds delay
// Stop music playback
myDFPlayer.stop();
buttonPressed = false;
}
} else {
buttonPressed = false;
}
}
Explain what trigger a relay and button together is? A relay would be an output and a button probably an input.
I have merged your cross-posts @ar5h.
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.


