Why is my Arduino freezing when using a relay board? - arduino

I'm working on a project where I use midi notes to switch 8 old lamps using the Arduino Uno. I've built a case with 8 wall sockets that are linked up to a relay board for the Arduino. I am using the Hairless midi serial bridge to send midi notes via USB to the Arduino.
This all works until I put power on the sockets with my Uno. After about 5~10 seconds the Arduino freezes. The relay shield stays in it's current state and the indication lights for serial communication stop flashing. When there isn't 220 volts going through the relays it all works great.
(My schematics are below.) The Arduino is powered via USB. I also tried powering the Arduino with an additional adapter of 5V and 500mA but that didn't make a difference.
Code:
#include <digitalWriteFast.h>
byte incomingByte=0;
byte notebyte=0;
byte velocitybyte=0;
byte statusbuffer=0;
byte NOTE_ON = 144;
byte NOTE_OFF = 128;
boolean arp_triggernext=false;
boolean firstbyte;
void MIDI_Poll(){
if (Serial.available() > 0) {
do {
// read the incoming byte:
incomingByte = Serial.read();
if (incomingByte>247) {
// this is where MIDI clock stuff is done
switch (incomingByte){
}
}
else if (incomingByte>240) {
statusbuffer = 0;
//sysex stuff done here
}
else if (incomingByte>127) {
statusbuffer = incomingByte;
firstbyte = true;
notebyte = 0;
velocitybyte = 0;
}
else if (statusbuffer!=0) {
if (firstbyte == true) {
// must be first byte
notebyte = incomingByte;
firstbyte = false;
}
else {
// so must be second byte then
velocitybyte = incomingByte;
//process the message here
if (statusbuffer == NOTE_ON && velocitybyte != 0) {
switch (notebyte) {
case 60:
digitalWriteFast2(2, HIGH);
break;
case 61:
digitalWriteFast2(3, HIGH);
break;
case 62:
digitalWriteFast2(4, HIGH);
break;
case 63:
digitalWriteFast2(5, HIGH);
break;
case 64:
digitalWriteFast2(6, HIGH);
break;
case 65:
digitalWriteFast2(7, HIGH);
break;
case 66:
digitalWriteFast2(8, HIGH);
break;
case 67:
digitalWriteFast2(9, HIGH);
break;
}
}
else if (statusbuffer == NOTE_OFF || (statusbuffer == NOTE_ON && velocitybyte == 0)) {
switch (notebyte){
case 60:
digitalWriteFast2(2, LOW);
break;
case 61:
digitalWriteFast2(3, LOW);
break;
case 62:
digitalWriteFast2(4, LOW);
break;
case 63:
digitalWriteFast2(5, LOW);
break;
case 64:
digitalWriteFast2(6, LOW);
break;
case 65:
digitalWriteFast2(7, LOW);
break;
case 66:
digitalWriteFast2(8, LOW);
break;
case 67:
digitalWriteFast2(9, LOW);
break;
}
}
//now clear them for next note
notebyte = 0;
velocitybyte = 0;
firstbyte = true;
}
}
} while (Serial.available() > 0);
}
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
MIDI_Poll();
}
It's based on code I've found for processing serial midi.
I'm really confused as to why this happens. I want to know why my Arduino and relay board freeze when there is 220 volts going through the relays.
Materials:
Arduino Uno revision 3
Relay board with 8 relays switched with 5 volt. Capable of handling 10A. Bought here: https://iprototype.nl/products/components/buttons-switches/relay-board-8-channels-5v
8 normal wallsockets powered with 220 volts
8 light bulbs with E27 fitting

As I am seeing here is some circuit issues so i am going to explain them 1 by 1
1. you should not run relay directly from Arduino pin so use appropriate circuit with transistor to run a relay and use arduino pin as a sink not a source will be better.
2.use a freewheeling diode with relay coil.
3.use optoisolator at Rx and Tx.

Related

Issue with using the adafruit motorshield v1 library

I am new to microcontrollers programming. I just got my hands on the arduino uno board. I am trying to build the popular "bluetooth robot car" using the uno, adafruit motorshield v1 and HC-05 bluetooth module. My code attached below compiles and uploads without any error or warning. While not stacking the shield and not attaching the bluetooth module, instead seeing "Robot remote control mode" on the serial monitor, I see some "Rbnld tedisjvv shhsjj hshhs". When I attach and connect the bluetooth module and send a command 'R', instead of seeing 'R' on the monitor I see 'R' followed by an up-pointing arrow. I have searched for help on the web on similar problems but none has helped. I have tried other codes for serial communication(like blinking an led when 'B' is read from serial) and it worked perfectly.I need help.
// include the Adafruit motor v1 library
#include <AFMotor.h>
AF_DCMotor MotorFR(1); // Motor for drive Front Right on M1
AF_DCMotor MotorFL(2); // Motor for drive Front Left on M2
AF_DCMotor MotorBL(3); // Motor for drive Back Left on M3
AF_DCMotor MotorBR(4); // Motor for drive Back Right on M4
const int buzPin = 2; // set digital pin 2 as buzzer pin (use active buzzer)
const int ledPin = A5; // set digital pin A5 as LED pin (use super bright LED)
int valSpeed = 255;
void setup(){
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("*Robot Remote Control Mode*");
pinMode(buzPin, OUTPUT); // sets the buzzer pin as an Output
pinMode(ledPin, OUTPUT); // sets the LED pin as an Output
// Set the speed to start, from 0 (off) to 255 (max speed)
MotorFL.setSpeed(valSpeed);
MotorFR.setSpeed(valSpeed);
MotorBL.setSpeed(valSpeed);
MotorBR.setSpeed(valSpeed);
// turn off motor
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
}
void loop() {
while (Serial.available() > 0) {
char command = Serial.read(); // gets one byte from serial buffer
Serial.println(command);
switch(command){
case 'F': // move forward
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'B': // move backward
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'R': // turn right
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(BACKWARD);
MotorBL.run(FORWARD);
MotorBR.run(BACKWARD);
break;
case 'L': // turn left
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(FORWARD);
MotorBL.run(BACKWARD);
MotorBR.run(FORWARD);
break;
case 'G': // forward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'H': // backward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'I': // forward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'J': // backward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'S': // stop
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
break;
case 'V': // beep buzzer
digitalWrite(buzPin, HIGH);
delay(150);
digitalWrite(buzPin, LOW);
delay(100);
digitalWrite(buzPin, HIGH);
delay(250);
digitalWrite(buzPin, LOW);
break;
case 'W': // turn light on
digitalWrite(ledPin, HIGH);
break;
case 'w': // turn light off
digitalWrite(ledPin, LOW);
break;
case '0': // set speed motor to 0 (min)
SetSpeed(0);
break;
case '1': // set speed motor to 30
SetSpeed(30);
break;
case '2': // set speed motor to 55
SetSpeed(55);
break;
case '3': // set speed motor to 80
SetSpeed(80);
break;
case '4': // set speed motor to 105
SetSpeed(105);
break;
case '5': // set speed motor to 130
SetSpeed(130);
break;
case '6': // set speed motor to 155
SetSpeed(155);
break;
case '7': // set speed motor to 180
SetSpeed(180);
break;
case '8': // set speed motor to 205
SetSpeed(205);
break;
case '9': // set speed motor to 230
SetSpeed(230);
break;
case 'q': // set speed motor to 255 (max)
SetSpeed(255);
break;
}
}
}
// function for setting speed of motors
void SetSpeed(int val){
valSpeed = val;
MotorFL.setSpeed(val);
MotorFR.setSpeed(val);
MotorBL.setSpeed(val);
MotorBR.setSpeed(val);
}```

Serial.read() - XBee - not firing 'else if'

I am currently trying to do some communication test between a PC and Arduino Uno using an XBee in AT mode.
My test is to send characters from the computer to the XBee and process through conditional statements.
I don't believe this issue is with configuration of the XBees, for I am able to communicate successfully when I watch the Serial monitors.
Here is the code I am running on the Arduino:
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
if (xBee.available()> 0)
{
if (xBee.read() == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if (xBee.read() == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
I can turn on the LED when sending the character 'r' from the PC to the XBee. The intended result is received back as well. When I try to send the character 'o' from the PC the LED stays on, and I get the response of "NR".
This same result happens with different characters in the else if statement, sending character 'o' as the first character, changing to just if statements, and changing the initial condition to - while xBee.available().
How can I fix this problem?
You need to store the input value of xBee.read() and then use it in the if condition.
You can try this
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
char read_value = xBee.read();
if(xBee.available()> 0)
{
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if ( read_value == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
The problem is that you are taking the input with xBee.read() but not storing it.
Only your first if works ie,
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
The control is not even going in the else if hence condition for o is not tested.

Communicating with Arduino Serial

I am trying to communicate with my Arduino over the USB port by using Serial:
int previous;
int current = 0;
void turnOn(int pinNumber){
previous = current;
current = pinNumber;
if(previous!=0){
digitalWrite(previous, LOW);
digitalWrite(current, HIGH);
}else{
digitalWrite(current, HIGH);
}
}
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
Serial.write(1);
}
void loop(){
delay(1);
if(Serial.available()>0){
switch(Serial.read()){
case 0:
turnOn(8);
break;
case 1:
turnOn(9);
break;
case 2:
turnOn(10);
break;
default:
Serial.println(Serial.read());
}
}
}
I am trying so that if I send a 0 the rightmost LED will light up, if I send 1, the middle one will and if I send a 2 the leftmost will. However when I send 0,1 or anything else it prints a -1 meaning the default switch has been triggered. How do I fix it?
Try this...
void loop(){
if (Serial.available()) {
char input = Serial.read();
if(input == '0'){
turnOn(8);
}else if(input == '1'){
turnOn(9);
}else if(input == '2'){
turnOn(10);
}
}
}
Tell me if it works or not then we can proceed :)

Arduino PINs not behaving equally

I have burned this code on my arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>
using namespace std;
#define BUFFSIZE 16
#define PIN0 0
#define PIN1 1
#define PIN2 2
#define PIN3 3
#define PIN4 4
#define PIN5 5
#define PIN6 6
#define PIN7 7
#define PIN8 8
#define PIN9 9
#define PIN10 10
#define PIN11 11
#define PIN12 12
#define PIN13 13
// For mac address please view the Ethernet shield.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD5};
IPAddress server(192, 168, 0, 61); // IP address of RAAS server
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 62);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 8 is selected for RAAS):
EthernetClient client;
String GetNextCommand()
{
int count,i;
int temp;
String command;
Serial.println("Waiting for next Command.");
while((count = client.available()) < 1 );
for(i = 0; i < count ; i++)
command += (char) client.read();
command.replace("\r","");
command.replace("\n","");
Serial.println(command);
return command;
}
bool ConnectServer() {
int resCount,i;
String response;
Serial.println("Trying to connect...");
// if you get a connection, report back via serial:
if (client.connect(server, 8)) {
Serial.println("connected");
// Make a Handshake request:
client.println("EHLO");
response = GetNextCommand();
if (response == "EHLO" ) {
Serial.println("Server Response OKAY");
return true;
}
} else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
return false;
}
void InitializeBoard() {
pinMode(PIN1, OUTPUT);
pinMode(PIN2, OUTPUT);
pinMode(PIN3, OUTPUT);
pinMode(PIN4, OUTPUT);
pinMode(PIN5, OUTPUT);
pinMode(PIN6, OUTPUT);
pinMode(PIN7, OUTPUT);
pinMode(PIN8, OUTPUT);
pinMode(PIN9, OUTPUT);
pinMode(PIN10, OUTPUT);
pinMode(PIN11, OUTPUT);
pinMode(PIN12, OUTPUT);
pinMode(PIN13, OUTPUT);
digitalWrite(PIN1, LOW);
digitalWrite(PIN2, LOW);
digitalWrite(PIN3, LOW);
digitalWrite(PIN4, LOW);
digitalWrite(PIN5, LOW);
digitalWrite(PIN6, LOW);
digitalWrite(PIN7, LOW);
digitalWrite(PIN8, LOW);
digitalWrite(PIN9, LOW);
digitalWrite(PIN10, LOW);
digitalWrite(PIN11, LOW);
digitalWrite(PIN12, LOW);
digitalWrite(PIN13, LOW);
}
void ParseCommand(String str) {
int pinNum;
int pinState = LOW;
String switchNo,operation;
int temp;
temp=0;
temp = str.indexOf(':',temp);
switchNo = str.substring(0,temp);
operation = str.substring(temp+1);//,str.length()-temp-2);
Serial.println("Port: " + switchNo);
Serial.println("Operation: " + operation);
if(operation == "OFF;")
pinState = LOW;
else if(operation == "ON;")
pinState = HIGH;
else Serial.println("Invalid Command from server!");
pinNum = str.toInt();
Serial.print("Setting ");
Serial.print( pinNum);
Serial.print( " to ");
Serial.println( pinState);
switch (pinNum){
case PIN1:
digitalWrite(PIN1, pinState);
break;
case PIN2:
digitalWrite(PIN2, pinState);
break;
case PIN3:
digitalWrite(PIN3, pinState);
break;
case PIN4:
digitalWrite(PIN4, pinState);
break;
case PIN5:
digitalWrite(PIN5, pinState);
break;
case PIN6:
digitalWrite(PIN6, pinState);
break;
case PIN7:
digitalWrite(PIN7, pinState);
break;
case PIN8:
digitalWrite(PIN8, pinState);
break;
case PIN9:
digitalWrite(PIN9, pinState);
break;
case PIN10:
digitalWrite(PIN10, pinState);
break;
case PIN11:
digitalWrite(PIN11, pinState);
break;
case PIN12:
digitalWrite(PIN12, pinState);
break;
case PIN13:
digitalWrite(PIN13, pinState);
break;
default:
Serial.println("Invalid Pin Address!");
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
} else {
Serial.println("Bound on given Mac");
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
//Keep trying to connect to the server until the server response okay!
while (!ConnectServer());
//While Server is connected, keep listening for incoming commands from server.
while (client.connected()) {
String command;
command = GetNextCommand();
ParseCommand(command);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Server Disconnected.");
client.stop();
}
}
Now I have to connect Relays on all the pins from 1-13. But pins are not behaving properly. When I send command 13:ON; or 13:OFF; pins on my Ethernet shield does not change its state in each case. But when I send 03:ON; or 03:OFF; it changes its state exactly w.r.t the command. Similarly some pins are responding like pin 3 and other pins are not (like pin13). Is this some thing to do with the code?
The issue is the Ethernet shield uses pins 13, 12, 11, as SPI . Also 10 and 4, to select SD or ethernet.
pinNum = str.toInt();
And then you have a switch statement to mach pinNum with the correct operation.
I would like to see how the value of str looks before "ParseCommand()" function. Can you show me what prints in the "GetNextCommand()"

Receiving SMS using GSM and controlling LED using Arduino

Has someone come up with a solution with the above stated problem?
We are using Arduino Duemilanove and SIM 900 GSM module (http://robokits.co.in/shop/index.php?main_page=product_info&products_id=303)
We've tried to work on the similar problem of lightning LEDs from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on. E.g. #a1b1c1d1 will switch on all the LEDs.
When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the LEDs lighten up. But if we send the sms with having content "#a1b1c1d1", we don't see any function of LEDs.
It would be great if anyone can give some guidance about the same.
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If #a1b1c1d1 comes as sms, all LEDs should light up.
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
//first led
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
//Second led
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
// Third led
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
//Fourth led
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
First do not use delay
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
This is neither necessary nor reliable. Instead of waiting some random time, you can check the network status with AT+CFUN and/or AT+COPS. If the GSM module is already attached to a network when you open the serial connection, it is a waste of time waiting like that. And if is not attached you should wait explicitly for that to happen (polling CFUN/COPS or enabling AT+CREG), otherwise you risk waiting too short time. See the 27.007 specification for more information for those commands.
Second do not use delay
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Please do not write code like this. See this answer on why using delay is such a bad idea, and this answer for suggestion to how to handle properly.

Resources