how to solve meaningless characters on serial montior Arduino - arduino-uno

Hi i am new at Arduino Uno , i am trying to use imu sensor and read datas from it so i installed mpu9500 lib from arduino website and run one example from its lib. However as i open the serial monitor, it displays strange characters . I check the pin configuration many times and load different libs and examples but i could not fix it.
Here is the codes i run.
#include "MPU9250.h"
MPU9250 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
if (!mpu.setup(0x68)) { // change to your own address
while (1) {
Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
delay(5000);
}
}
}
void loop() {
if (mpu.update()) {
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 25) {
print_roll_pitch_yaw();
prev_ms = millis();
}
}
}
void print_roll_pitch_yaw() {
Serial.print("Yaw, Pitch, Roll: ");
Serial.print(mpu.getYaw(), 2);
Serial.print(", ");
Serial.print(mpu.getPitch(), 2);
Serial.print(", ");
Serial.println(mpu.getRoll(), 2);
}
And this is what i get also i changed value of boud but nothing changed
01:10:23.688 -> ⸮7в⸮⸮⸮⸮⸮⸮`f⸮⸮⸮⸮⸮f⸮⸮⸮~⸮⸮xxf⸮~⸮⸮fx⸮⸮⸮⸮⸮⸮怘⸮

Try running simple Serial print code at the same baud rate(115200) and check if your board is alright. Then run the following code once:
#include "MPU9250.h"
MPU9250 mpu; // You can also use MPU9255 as is
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup(0x68); // change to your own address
}
void loop() {
if (mpu.update()) {
Serial.print(mpu.getYaw()); Serial.print(", ");
Serial.print(mpu.getPitch()); Serial.print(", ");
Serial.println(mpu.getRoll());
}
}

Related

Send string on serial monitor Arduino tinkercad

I have problem with my code. I wanna send alphanumeric on serial monitor Arduino. I hope someone can help
void setup()
{
Serial.begin(9600);
}
void loop()
{
if ( Serial.available())
{
char datachar = Serial.read();
}
delay(1000)
serial.print(datachar)
}
Your code will not compile because of the following errors:
The statements delay(1000) and serial.print(datachar) are missing the ; at the end.
The variable datachar is declared inside the if block, so it will not be available outside when you call serial.print(datachar).
You misspelled the word serial in serial.print(datachar), it should start with an uppercase letter.
I believe this is what you are looking for:
void setup()
{
Serial.begin(9600);
}
void loop()
{
char datachar;
if(Serial.available())
{
datachar = Serial.read();
}
delay(1000);
Serial.print(datachar);
}

Attiny85 with ArduinoUno for I2c comm

I am working on attiny85 for I2C communication. I have gone through different libraries already like Wire.h, TinyWire.h, tinyWireM.h, tinyWireS.h.
In the start I want to send some byte of data through I2C comm and tried to scope the pin with oscilloscope but its not giving me the appropriate results. Looking on the internet about different ways to make attiny85 work with I2c is really heartless and I could not achieve the task. Finally, I tried to make attiny85 as master and arduino Uno as slave as it was spare in my box.
I tried to make attiny85 as master and send data to arduino and looks the output on serial monitor but its showing zero.
For the reference, the master and slave codes are attached and my task is just simple to check on serial.
Attiny85 as Master
#include <TinyWireM.h>
void setup()
{
TinyWireM.begin();
}
void loop()
{
TinyWireM.begin();
TinyWireM.beginTransmission(0x08);
TinyWireM.send(0x99);
int Byte1 = TinyWireM.endTransmission();
delay(1000);
}
Arduino as Slave
#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
Wire.begin(add);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
Serial.println ("Data receiving");
Serial.println(byte1);
delay(1000);
}
void receiveEvent(int bytes)
{
byte1 = Wire.read();
}
But I am not able to get the output on serial monitor of arduino.
What am i doing wrong here?
I have used Atiny85 as a slave using TinyWireS lib (https://github.com/nadavmatalon/TinyWireS) some time back and it worked fine.
Below were the pin configurations
ATtiny85 pin 5 with Arduino Uno A4 and
ATtiny85 pin 7 with Arduino Uno A5
Below are my codes
Atiny.
#include "TinyWireS.h"
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
volatile byte data = { 0, 1, 2, 3 };
void setup() {
TinyWireS.begin(SLAVE_ADDR);
TinyWireS.onRequest(requestISR);
}
void loop() {}
void requestISR() {
for (byte i=0; i<NUM_BYTES; i++) {
TinyWireS.write(data[i]);
data[i] += 1;
}
}
Uno.
#include <Wire.h>
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
byte data[NUM_BYTES] = { 0 };
byte bytesReceived = 0;
unsigned long timeNow = millis();
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print(F("\n\nSerial is Open\n\n"));
}
void loop() {
if (millis() - timeNow >= 750) { // trigger every 750mS
Wire.requestFrom(SLAVE_ADDR, NUM_BYTES); // request bytes from slave
bytesReceived = Wire.available(); // count how many bytes received
if (bytesReceived == NUM_BYTES) { // if received correct number of bytes...
for (byte i=0; i<NUM_BYTES; i++) data[i] = Wire.read(); // read and store each byte
printData(); // print the received data
} else { // if received wrong number of bytes...
Serial.print(F("\nRequested ")); // print message with how many bytes received
Serial.print(NUM_BYTES);
Serial.print(F(" bytes, but got "));
Serial.print(bytesReceived);
Serial.print(F(" bytes\n"));
}
timeNow = millis(); // mark preset time for next trigger
}
}
void printData() {
Serial.print(F("\n"));
for (byte i=0; i<NUM_BYTES; i++) {
Serial.print(F("Byte["));
Serial.print(i);
Serial.print(F("]: "));
Serial.print(data[i]);
Serial.print(F("\t"));
}
Serial.print(F("\n"));
}

Unicast image transmission using Xbee

community
I've been working on image transmission using xbee s2b pro modules and Arduino Mega. Main task is to transmit an jpg image taken by a jpeg serial camera at the transmitter and send it to a microSD memory at the receiver, but I've been dealing with one unique trouble, I need a delay of 2 seconds to send and receive succesfully 1 byte, if I set less seconds I lose information and receipt image get corrupted. Here you will see my codes:
Transmitter code:
byte ZERO = 0x00;
byte incomingbyte;
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial1.begin(38400);
}
void loop()
{
byte a[32];
int ii;
SendResetCmd();
delay(4000);
SendTakePhotoCmd();
delay(1000);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
}
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20);
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{
Serial.write(a[j]);
delay(2000);// observe the image through serial port
}
i++;
}
while(1);
}
void SendResetCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x26);
Serial1.write(ZERO);
}
void SetImageSizeCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x31);
Serial1.write(0x05);
Serial1.write(0x04);
Serial1.write(0x01);
Serial1.write(ZERO);
Serial1.write(0x19);
Serial1.write(0x11);
}
void SetBaudRateCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x24);
Serial1.write(0x03);
Serial1.write(0x01);
Serial1.write(0x2A);
Serial1.write(0xC8);
}
void SendTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(ZERO);
}
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x32);
Serial1.write(0x0c);
Serial1.write(ZERO);
Serial1.write(0x0a);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(MH);
Serial1.write(ML);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(0x20);
Serial1.write(ZERO);
Serial1.write(0x0a);
a+=0x20;
}
void StopTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(0x03);
}
Receiver code:
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 53; // Pin 10 on Arduino Uno
void setup()
{
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
if (!SD.begin()) {
return;
}
}
void loop() {
byte buf[5000];
if (Serial.available()>0)
{
myFile = SD.open("imgrx.jpg", FILE_WRITE);
Serial.readBytes(buf,sizeof(buf));
Serial.println((byte)*buf);
myFile.write((byte)*buf);
while(Serial.available()>0) Serial.read();
}
else{
myFile.close();
}
}
I've tried a MicroSD -> XBEE -> MicroSD also and that trouble continued there, maybe I've passed through something and I'd like to tell me where. Let me add these methods give me images identical to the ones I send, so even slowly, it's very effective. I changed baudrates without any success. Xbee adresses are connected as unicast. I also tried using only Serial.readBytes and I got Bytes faster but as decimal values and is unreadable.
I would really appreciate any information or experience approach to solve this problem. Ask me anything you don't understand about my question. Thanks
Some starting recommendations:
Increase your baud rate to 115200 to increase throughput to/from the XBee.
Add hardware flow control to your setup so you know when the XBee module is "full" of queued data and not receiving.
Switch to API mode on your XBee modules, and generate API frames using the maximum payload size.
Wait for the Transmit Status response on your outbound Transmit frames before sending your next packet.
XBee 802.15.4 modules (including Zigbee and DigiMesh) weren't designed for high throughput. They're low-power, long range devices where you'll be lucky to get 10kbytes/second.

RPi + ESP8266 stability issues

I was recently working on a home automation project which has finally come to end and I am thinking to install it in my home. First of all, I would like to brief you with the basic architecture.
I am using a Raspberry Pi 3 as the central controller node which is running Node-Red and its Mosca palette. Currently, there are 5 ESP-01 in the project. Four of them are wired up with a relay and the remaining ESP is wired up with a DHT11 temperature sensor. Almost everything is running good but I am facing some stability issues, like, when I recycle the power the ESP-01 doesn't run the program. Serial monitor stays blank. However, when I disconnect the GPIO2 from the relay and then power up the ESP. The program begins. So, I have to pull out the GPIO2 then power up the ESP then connect the GPIO2 with the relay on every power recycle. Another problem which I am facing is sometimes the ESP crashes automatically. It sometimes prints out fatal exception(0) or soft wdt reset even though I have added a watchdog timer.
Here is the code for one of the client ESP:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "........";
const char* password = ".........";
const int led = 13;
const char* mqtt_server = "192.168.1.8";
WiFiClient espClient;
PubSubClient client(espClient);
const int ledGPIO2 = 2;
void setup_wifi() {
int i;
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("WIFI BEGUN");
while (WiFi.status() != WL_CONNECTED) {
ESP.wdtFeed();
delay(500);
i++;
if ((i&0x01)==0){
digitalWrite(led, 0);
} else {
digitalWrite(led, 1); // led should start blinking at .5 seconds
}
Serial.print(".");
if (i>1000) break; // get out after 50 seconds
if (i==1000){
}
Serial.print(".");
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
}
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if(topic=="Lamp1"){
Serial.print("Changing GPIO 2 to ");
if(messageTemp == "on"){
digitalWrite(ledGPIO2, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(ledGPIO2, LOW);
Serial.print("Off");
}
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.subscribe("Lamp1");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(ledGPIO2, OUTPUT);
digitalWrite(ledGPIO2, true);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
}
Also, I have been thinking for an efficient power supply for ESP. Batteries cannot be reliable for long-term and powering up via an adapter would be unfeasible as the module is going to be mounted on the wall. Moreover, ac to dc converter was something which seems to be a decent way for power supply.
Vcc - 3.3V
CH_PD - 3.3V
Tx - Tx (Arduino)
Rx - Rx (Arduino)
GPIO0 - GND (while uploading the sketch)/ 3.3V
GND - GND
I am not using capacitors or resistors. I am getting a 5V supply from Arduino which is regulated to 3.3V using LD33V voltage regulator.
Any suggestions would be appreciated. Thank You!!

Measuring Time of Flight with Arduino Uno

I am trying to implement a master/slave setup to determine the time of flight between two Arduino Uno boards and ultimately use that as a measure of distance between the two. Using the standard 16MHz crystal and the APC220 from DFRobot communicating between the two is easy but getting a time of flight reading is where I get stuck.
I use the following code on the master side to send the first signal and receive the echo from the slave:
// set pins:
const int switchPin = 3; // pin number of the switch
// variables:
int switchState = 0; // variable for reading switch status
int iDisplay = 1;
unsigned long start, finished, elapsed;
// initialize
void setup()
{
// initialize switch pin as input:
pinMode(switchPin, INPUT);
// initialize serial wireless communication:
Serial.begin(9600);
// read initial state of switch
switchState = digitalRead(switchPin);
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;
Serial.print("Raw elapsed time: ");
Serial.println(elapsed);
Serial.print("Elapsed time: ");
Serial.print(h,0);
Serial.print("h ");
Serial.print(m,0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.println();
}
// program loop
void loop()
{
if (Serial.available()>0 && iDisplay == 1)
{
finished=millis();
displayResult();
iDisplay = 0;//Only once
}
// read switch state and print line if state has changed
switch (digitalRead(switchPin)) { // read pin status
case HIGH:
if (switchState == LOW)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = HIGH; // message has been sent
}
break;
case LOW:
if (switchState == HIGH)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = LOW; // message has been sent
}
break;
}
}
And the following code for the slave:
// variables:
int intTime = 0;
// initialize
void setup()
{
// initialize serial wireless communication:
Serial.begin(9600);
}
// program loop
void loop()
{
if (Serial.available()>0)
{
intTime = Serial.parseInt();
if (intTime > 1)
{
Serial.println(intTime);
}
intTime = 0;
}
}
Yet this only returns the 200 Milliseconds from the debounce delay, can this be done with the Arduino? Am I getting the math or the code wrong?

Resources