I'm really confused.
I have a Raspberry Pi 3B+ that needs to communicate to an Arduino Nano by using its GPIO
pins RX & TX
this is working fine except for a logic issue that I cannot figure out. I have a simple java web interface with 2 buttons, namely on and off, this interacts with a python script that needs to issue commands to the Arduino and it needs to either turn on or turn off a relay depending on the command it receives. By printing the state
the arduino receives does reflect what the arduino should do (On button sends on command and Off button sends off command)
But, when I click a button the list below happens,
I printed the state of the pin to which the relay is connected and its state does weird stuff,
#define relayPin 8
String command;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if(Serial.available()){
command = Serial.readStringUntil('\n');
command.trim();
if(command == "on"){
turnOn();
}
if(command == "off"){
turnOff();
}
}
}
void turnOn(){
print();
if (digitalRead(relayPin) == 0) {
digitalWrite(relayPin, HIGH);
}
}
void turnOff(){
print();
if (digitalRead(relayPin) == 1) {
digitalWrite(relayPin, LOW);
}
}
void print(){
Serial.println("Pin State: ");
Serial.print(digitalRead(relayPin));
Serial.println();
}
For the Arduino I have the following
#define relayPin 8
String command;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if(Serial.available()){
command = Serial.readStringUntil('\n');
command.trim();
if (command.equals("on")) {
digitalWrite(relayPin, HIGH);
}
else if (command.equals("off")) {
digitalWrite(relayPin, LOW);
}
}
}
Initially I had this, and used a USB cable for communication and it worked perfectly
#define relayPin 8 String command; void setup() { Serial.begin(9600); pinMode(relayPin, OUTPUT); } void loop() { if(Serial.available()){ command = Serial.readStringUntil('\\n'); command.trim(); if (command.equals("on")) { digitalWrite(relayPin, HIGH); } else if (command.equals("off")) { digitalWrite(relayPin, LOW); } } }
I used this tutorial to configure the raspberry to allow serial communication by means of GPIO
https://github.com/Arijit1080/Raspberry-Pi---Arduino-Serial-Communication[tutorial][1]
and in this I did everything that the instructions.txt
specified, namely
1. sudo nano /boot/config.txt dtparam=spi=on dtoverlay=pi3-disable-bt core_freq=250 enable_uart=1 force_turbo=1 2. sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt 3. sudo nano /boot/cmdline.txt dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles 4. sudo reboot 5. ls -l /dev sudo systemctl stop serial-getty@ttyAMA0.service sudo systemctl disable serial-getty@ttyAMA0.service 6. sudo apt-get install minicom 7. minicom -b 9600 -o -D /dev/ttyAMA0
And this is how everything is wired up
What could the problem be?
It turns out that the relay is the main problem, when I give it a HIGH
signal, the coil does not stay energized until it receives a LOW
signal as my previous relay does, instead, I need to send a pulse signal if you will, when I do the following it seems to work as expected
#define r 8
String command;
void setup() {
Serial.begin(9600);
pinMode(r, OUTPUT);
}
void loop() {
command = Serial.readStringUntil("/n");
command.trim();
if (action.equals("switch")) {
digitalWrite(r, HIGH);
delay(100);
digitalWrite(r, LOW);
}
}
When I send a command switch
it energizes the coil and releases it again, this causes the relay to switch, when I send the same command, it then switches back. As mentioned, the coil doesn't stay energized so that I can see what state it's in.
I only need to figure out a way to get some sort of status back to see what the relay is doing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.