Receive XBee signals on Arduino - serial-port

Side A:
Arduino Uno R3 with Wireless Proto shield powered with USB
With XBee Pro S1, DH 0 DL FFFF MY 0 API 0
Wireless Proto shield has the serial select switch on the 'micro' position
side B:
XBee Explorer USB connected to a PC with XCTU software
With XBee Pro S1, DH 0 DL FFFF MY 0 API 0
(When I put both XBee modules in the USB explorer boards, connected with two PC's, I can send data back and forth without any problems, so I reckon the XBee settings are good.)
The Problem
Now I want the Arduino to capture the input from the B side (send with the XCTU terminal), but when I type anything in the terminal, the RSSI LED on side A just turns on for 5 seconds, but the Arduino does not seem to capture any data since it does not send data back like it should (Serial.print("I received: ");
Arduino sketch:
int incomingByte = 0;
void setup() {
Serial.begin(19200); //Both XBee chips are configured at 19200 Baud
Serial.print("start echo machine"); //This is received just fine on the B side
}
void loop() {
if (Serial.available() > 0) {
// Read the incoming byte:
incomingByte = Serial.read();
// Say what you got:
Serial.print("I received: "); //This never shows on the B-side
Serial.println(incomingByte, DEC);
}
}
How do I fix this problem?

You have to use a SoftwareSerial(RX,TX) for the XBee and the Serial for printing the output into the pc.
RX and TX of SoftwareSerial must be linked to the DOUT and DIN pin of the module into the Wireless Proto shield:
#include <SoftwareSerial.h>
// Connect pin 10 of Arduino to DOUT of Wireless Proto shield
uint8_t ssRX = 10;
// Connect pin 11 of Arduino to DIN of Wireless Proto shield
uint8_t ssTX = 11;
SoftwareSerial nss(ssRX, ssTX);
void setup() {
Serial.begin(19200);
nss.begin(19200);
Serial.println("Serial works");
}
void loop() {
if (nss.available()){
Serial.println("received packet:");
for(int i=0;i<25;i++){
Serial.print(nss.read(),HEX);
Serial.print(",");
}
Serial.println();
}

Many of the boards require the pull-up resistor on DIN to be enabled.
According to some sources this pull-up is enabled by default on the Digi Xbee module.
To ensure it is enabled or to enable it:
Put your Xbee module in a USB explorer and use X-CTU to check the PR configuration.
DIN is on bit 7 for the Xbee Pro S1, so in that case you need the last bit to be 1.
I put it like this: 00000001
Than you convert it to hex (01 in my case) and write that value to the Xbee module with X-CTU.
So it is an electronics issue and not a programming issue.

Related

Arduino communication with TMC2209

I'm trying to communicate with TMC2209 (stepper drive) with an Arduino nano Every.
I connected pin RX on pin D2 and TX on pin D3.
I placed a 1K resistor between TX and RX.
It seems I can write parameters (even I'm testing this deeply, I'm not so sure now..) but I'm not able
to read nothing from driver.
Picture added 15/04/2021 related to datasheet of TMC2209
In my test, I tried with only one driver with address 0, means MS1_AD0 and MS2_AD1 connected to GND.
driver1.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
I tried reading IFCNT and DRV_STATUS, always ZERO.
The initialization is done in this way:
#include <Wire.h>
#include <AG_AS5600.h>
#include <TMCStepper.h>
#include <AccelStepper.h>
#define ENCODER false
#define FEEDBACK_I2C true
#define GEARBOX 139 //51
#define MICROSTEPS 16
#define MIN_SPEED 20
#define MAX_SPEED 3000
double offsetHome = 77.0;
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SerialPort SerialUSB
#define SYS_VOL 3.3
#else
#define SerialPort Serial
#define SYS_VOL 5
#endif
#define SW_RX 2 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX 3 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define EN_PIN 7 // Enable
#define DIR_PIN 8 // Direction
#define STEP_PIN 9 // Step
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS1 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
// SilentStepStick series use 0.11
// UltiMachine Einsy and Archim2 boards use 0.2
// Panucatt BSD2660 uses 0.1
// Watterott TMC5160 uses 0.075
AG_AMS_5600 ams5600;
TMC2209Stepper driver1(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS1);
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
String cmd = {};
void setup()
{
SerialPort.begin(115200);
Wire.begin();
Wire.setTimeout(10);
SerialPort.println(">>>>>>>>>> Program started <<<<<<<<<<");
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Enable driver in hardware
// Enable one according to your setup
SPI.begin(); // SPI drivers
driver1.beginSerial(115200); // SW UART drivers
driver1.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driver1.toff(5); // Enables driver in software
driver1.rms_current(2000); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver1.microsteps(MICROSTEPS);
driver1.pwm_autoscale(true); // Needed for stealthChop
stepper.setMaxSpeed(degToSteps(500.0)); // steps/s
stepper.setAcceleration(degToSteps(1.0)); // steps/s^2
stepper.setEnablePin(EN_PIN);
stepper.setPinsInverted(false, false, true);
stepper.disableOutputs();
}
void loop()
{
unsigned long t0 = micros();
switch (step) {
case 0:
cmd = SerialPort.readString();
if (cmd.charAt(0)=='a' && cmd.charAt(1)=='=') {
acc = degToSteps(cmd.substring(2).toDouble());
SerialPort.print("Acceleration=");
SerialPort.println(acc);
cmd = "";
}
else if (cmd.charAt(0)=='d' && cmd.charAt(1)=='=') {
dec = cmd.substring(2).toInt();
SerialPort.print("Deceleration=");
SerialPort.println(dec);
cmd = "";
}
else if (cmd.charAt(0)=='c' && cmd.charAt(1)=='?') {
driver1.microsteps(MICROSTEPS);
driver2.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Counter2=");
SerialPort.print(driver2.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
cmd = "";
}
}
The hardware should be ok because the jumper for UART is configured as default on pin4.
Can someone help me to understand why?
Thanks
Andrea
I found!
The issue was the handling of "software" serial port.
Unfortunately I was confused because many examples of "TMCstepper.h" library are shown using two pins as TX and RX, so I was convinced internally pins were handled to send and receive.. but it's not.
So solution I found is:
#include <SoftwareSerial.h>
SoftwareSerial SerialDriver1(SW_RX, SW_TX);
TMC2209Stepper driver1(&SerialDriver1, R_SENSE, DRIVER_ADDRESS1);
void setup()
{
SerialDriver1.begin(57600);
SerialDriver1.listen();
...
and was really important to comment:
//driver1.beginSerial(115200); // SW UART drivers
this is important I guess because the port was already opened in the setup by me.
and it's necessary to specify the software port where to listen, not done by TMC library.
Last hint, with Arduino Nano Every I found out that Software Serial seems to work with max baudrate of 57600, 115200 was not working.

DS1302 RTC board weird outputs on Arduino's Serial monitor

I have a DS1302 RTC board (Waveshare) connected to an Arduino uno.
I'm printing time to Arduino's Serial Monitor but I get weird numbers/characters, and after 2-4 seconds it stops printing.
Wiring:
Vcc -> 5v
GND -> GND
I/O (MISO) -> Pin 12
SCLK -> Pin 13
CE (CS) -> Pin 10
Library used: VirtuabotixRTC library.
Things I've tried so far:
I tried a second DS1302 RTC board.
I tried a different Arduino board.
I tried changing the jumper wires.
I tried different baud rates.
Code:
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(7,8,9);
void setup() {
Serial.begin(9600);
// myRTC.setDS1302Time(30,30,5,5,5,5,2020);
}
void loop() {
myRTC.updateTime();
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
}
Screenshots:

Blinking an LED linked through Arduino-Python

I wrote a sample code for blinking an LED linked to Python. The code is not throwing me any error, but the LED is not blinking. Any suggestions?
Python code:
import serial #import the pyserial library
connected = False #this will represent whether or not ardunio is connected to the system
ser = serial.Serial("COM3",9600) #open the serial port on which Ardunio is connected to, this will coommunicate to the serial port where ardunio is connected, the number which is there is called baud rate, this will provide the speed at which the communication happens
while not connected:#you have to loop until the ardunio gets ready, now when the ardunio gets ready, blink the led in the ardunio
serin = ser.read()
connected = True
ser.write('1') #this will blink the led in the ardunio
while ser.read() == '1': #now once the led blinks, the ardunio gets message back from the serial port and it get freed from the loop!
ser.read()
print('Program is done')
ser.close()
Arduino code:
void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
Serial.write('1');
}
void loop() {
if(Serial.available()>0){
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
Serial.read();
}
else{
Serial.available()==0;
}
}
In the arduino code, you call
digitalWrite(50,LOW);
and
digitalWrite(255,HIGH);
but the first parameter of digitalWrite is the pin number, which you defined as pin 10. Simply change 50 and 255 to 10 as that is where you want your low and high signals to output to.

Can't connect Arduino to RFID

I am using an A-Star 32U4 Micro Arduino and I'm trying to connect the RDM6300 - 125KHz Cardreader Mini-Module.
I'm using this sketch at the moment:
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void loop() {
// check, if any data is available
if(Rfid.available() > 0 ){
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
}
}
With this circuit:
module TX --- Arduino pin 2
module VCC ----- 5v
module ground ---- ground
antenna pins ---- antenna
When I put the card in the sensor nothing shows up on serial port. I tried this setup and the exact same sensors on an Arduino Uno (same sketch) and it worked perfectly, but I cant get this working on the Micro.
Arduino UNO and Micro uses different processors, though they work fairly similarly, they are not totaly identical.
It seams that
not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
From the SoftwareSerial Library description ( https://www.arduino.cc/en/Reference/softwareSerial )
Change the module TX from pin 2 to pin 8. and you should be good. :-)

How to connect Arduino Micro to Ethernet module ENC28J66 and run EtherCard BackSoon example?

I have arduino micro (oryginal) like this: https://www.arduino.cc/en/Main/ArduinoBoardMicro and clone of ethernet bord with ENC28J66 chipset.
Pin configuration on ethernet board:
CKOUT
/INT
/WOL
SO
SI
SCK
/CS
/RESET
VCC (5V) (it is also possible to use 3.3V after soldering one place)
GND
I have try to run example BackSoon for EtherCard library with few combination of conection but this gave me 'Failed to access Ethernet controller'
Then i set it like this:
Micro | ENC28J60
SCK - SCK
MISO - SO
MOSI - SI
SS - CS
5V - VCC
GND - GND
10 PIN - INT
This config don't generate access error but DHCP failed. DHCP server configuration is good for sure.
Even if i set static IP address there is no respons on this IP from http like in example.
Full code of my try for dhcp:
#include <EtherCard.h>
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 172,16,10,222 };
// gateway ip address
static byte gwip[] = { 172,16,10,1 };
#endif
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
const char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Service Temporarily Unavailable"
"</title></head>"
"<body>"
"<h3>This service is currently unavailable</h3>"
"<p><em>"
"The main server is currently off-line.<br />"
"Please try again later."
"</em></p>"
"</body>"
"</html>"
;
void setup(){
Serial.begin(57600);
while (!Serial) ;
Serial.println("\n[backSoon]");
if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
}
void loop(){
// wait for an incoming TCP packet, but ignore its contents
if (ether.packetLoop(ether.packetReceive())) {
Serial.println("got one!");
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
}
Serial response:
[backSoon]
DHCP failed
IP: 0.0.0.0
GW: 0.0.0.0
DNS: 0.0.0.0
Can someone help me to propellery connect one to another and run any sample (like BackSoon)?
Is it possibile to use Micro and ENC28J60 together?
Is it possibile to use EtherCard library for them?
Long time is passed from your question, but I hope this can help someone.
Micro - Ethernet
SCK - SCK
MISO - SO
MOSI - SI
PIN 8 - CS (8 is the default value for ethercard, 10 in your code)
5V - VCC
GND - GND

Resources