Hello, this is my first post here so I hope I got the right category since I have more than one problem.
I wanted to build a circuit that waters a plant via a water pump (3-6V) with a relay, a soil moisture sensor, an ultrasonic that helps me measure how much water is there left, a temperature module sensor (KY 028, I've programmed it to show the approximative room temperature) and I wanted to print all of these sensors' information on a led matrix display ( MAX7219 8x8 with 4 devices). Also, I have an arduino uno R3 CH340.
Without the led matrix, the whole circuit and code works fine, but it makes some of the sensors pointless because they're of no use if I can't see the values, and on its own with some sensors (meaning no water pump and relay, just the led matrix and some sensors) it also works fine, so none of the components are broken.
Now, enter the problem: when I put all of the components together, the led matrix has all LEDs on and just flickers as the relay switches on and off.
To counter this problem I've tried connecting a 9V battery to the pump and relay, but to no avail, the led matrix display ..well, displays the same problem, all LEDs on and flickers as the relay switches. To top it off, last time I've tried to upload the code with the led matrix display attached, I've also got an avrdude error. I have checked possible troubleshooting methods online (no wires on pins 0 or 1, drivers work well since I've uploaded code that works well on the arduino multiple times, though I haven't tried uploading the code with Programmer AVR ISP while I had the led matrix display on, I was afraid I'd burn it if I tried more than I already had)
avrdude: stk500_paged_load(): (a) protocol error, expect=0x14, resp=0x1c
avrdude: stk500_cmd(): programmer is out of sync
avr_read(): error reading address 0x0000
read operation not supported for memory "flash"
avrdude: failed to read all of flash memory, rc=-2
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0xf4
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0xf4
Is there anything I can do to make the led matrix print what I want it to?
Also, note worthy, I don't posses any other components other than the ones in the scheme I have attached aside from another led matrix display, but with only one device. And I don't think it's a code issue either, maybe electrical one, but I'll also attach the code anyway. If I cut all led matrix display lines and leave the last block uncommented, the code works just fine.
Code:
//ultrasonic
int trigPin = 11;
int echoPin = 12;
//matrice led
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 9
#define DATA_PIN 10
#define CS_PIN 8
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// pompa apa + moisture sensor
int ACWATERPUMP = 13;
int sensor = A0;
int valUmid = 0;
//modul temperatura
int tempPin = A1;
int analogTempVal = 0;
void setup() {
//ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//pompa apa
pinMode(ACWATERPUMP, OUTPUT);
//matrice
P.begin();
Serial.begin(9600);
}
void loop() {
//for debug purposes, to not have the pump spraying water while I fix the led matrix
digitalWrite(ACWATERPUMP, HIGH);
delay(500);
//ultrasonic
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 3900 ) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
//masurare temp
analogTempVal = analogRead(tempPin);
//Serial.println(analogTempVal);
int trueTemp = 0;
trueTemp = (analogTempVal*4-400-23*25)/(-25);
Serial.println(trueTemp);
delay(500);
P.print("temp: ");
P.print(trueTemp);
delay(500);
// //if moisture value over 450, turn on pump
// valUmid = analogRead(sensor);
// //Serial.println(valUmid);
// if(valUmid > 450)
// {
// digitalWrite(ACWATERPUMP, LOW);
// }
// else
// {
// digitalWrite(ACWATERPUMP, HIGH);
// }
// delay(400);
}