When i first bought the Mega arduino 2560 by "ELEGOO" it worked perfectly fine but after like 1-2 days it just stopped working like i putted the usb then uploaded the code and didnt work i reseted it too but still didnt work i checked if it was a wiring problem but it wasnt i checked the components with the multimeter and they resulted working but they still didnt work on the breadboard or without it.
When i load a code the yellow light "L" closes and "TX" and "RX" flash together and the "L" lights back on
Well starting with the "L" LED. The L LED is attached to I/O pin 13 via a resistor to ground . If it flashes, it's because a sketch is doing it under program control. Many UNOs come with the flash sketch pre-loaded, so the LED flashes.
Since you have not posted any code that you are loading I have no idea why the "L" LED is blinking or On. What code have you loaded and anything telling pin 13 to be a logic high? No code, no project description and no schematic of what you have connected? This is as good as it gets. The forums have sections explaining how to get good help, I suggest you read them.
i used this code the first time it started not working
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 51 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 50 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
i connected VCC to 5V, Trigger to 51, Echo to 50 and GND to Ground but still didnt say anything in the Serial monitor of Arduino IDE and yes i have NewPing Library installed
I can share this much. I loaded your code on my Arduino Mega 2560 Arduino board and the code loads fine. I don't have a ultra sonic distance module but the code loads fine and in my serial monitor I just read PING: 0cm which is what I would expect. The TX LED is rapidly blinking which I would expect and the RX LED is not doing anything which is also what I would expect. I just let the board be powered off the USB port but that should not matter. The L LED is constant on and I have no idea why. I did notice that if in my Setup code I include:
pinMode(13, OUTPUT); // sets the digital pin 13 as output
and in my run code I include:
digitalWrite (13, LOW);
The LED remains off so I am not sure why it has a default HIGH on pin 13 the onboard LED pin. Anyway your code seems to load and run on my Arduino MEGA board. I would disconnect everything and just try to run a simple blink code and blink the onboard LED just to see if the board is functional. Try this example code:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
The Onboard yellow LED should blink at 1 sec intervals. That should give an indication that your board is working.
when i put the led as a blinking setup and put cathode (shorter leg) to 13 and anode (longer leg) with a 220 ohm resistor to GND (Ground) the "L" is open but weak (low opacity), and when i run the code (upload) the led on the breadboard flashes as "TX" and "RX" and when i reset it, it lights up for not even a second it closes and does that another time and that happens on "L" too, so i dont know whats the problem.
You did not have to add a LED to pin 13. Pin 13 on the Maga board is an onboard LED (the yellow one). When we load code the TX and RX LEDs will flash a bunch, that is normal. Once the blink code is loaded the L (Yellow LED) should blink at a 1 sec interval. This is a simple blink code routine.
Next:
You do not want anything else on Pin 13. What is likely happening is you are overloading the pin. A typical plane Jane LED if we assume 5.0 volts logic High is 5.0 volts. A basic Red LED will typically be about 2.0 volts with 20 mA max current. So if you are using a RED LED for example I see 5.0 Volts applied - 2.0 volts (LED FWD Voltage) / 0.020 mA = 150 Ohms so your 220 Ohms should be fine but you are also driving the onboard LED and you may be overloading the pin. The idea here is just to see if the board works running a simple blink code routine with nothing else connected to the MEGA board. You can just power the board using the USB port you are connecting to. Just run the code and nothing connected to the board other than USB power.
If you want to connect a simple LED connection just connect the LED cathode to ground anode to your series resistor and resistor to your pin of choice. The main consideration is making sure we never exceed what any pin can sink or source. You really want to keep the current below 20 mA to be on the safe side. I mentioned it earlier but with a LED your current limiting resistor is just a matter of your Vsupply (supply voltage) - your LED Fwd Voltage / LED Current. Just about all LEDs will do fine below their maximum current. A good LED (quality) will have a data sheet which covers Vfwd and Max Current and you never run a LED at Max. Doing so will just lead to a short LED life.