Read Received via SPI data with arduino mega - c++

There
I am working on one of the project, in that i am trying to read 16-bit data from ADS8681. Currently i get the correct waveform of SCk,MISO,MOSI and SCK on oscilloscope. But I am not able to print data on alphanumeric LCD. With readRegister(long thisRegister, long bytesToRead) also unable to print data. Can anyone suggest me how i print received data?
References which i am using:
1] https://www.arduino.cc/en/Tutorial/LibraryExamples/BarometricPressureSensor#code
Source Code :
#include <SPI.h>
#include <LiquidCrystal.h>
#define SS 53 // CS/CONVST
LiquidCrystal lcd (38,37,36,35,34,33);
void setup()
{
lcd.begin(16,4);
SPI.begin();
digitalWrite(SS,OUTPUT);
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x14, 0x0001); //Write data range selection
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x10, 0x4100); //Write Data_OUT_CTRL
lcd.setCursor(0,0);
lcd.print ("Data");
}
void loop()
{
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x14, 0x0001); //Write data range selection
delay(100);
}
void readRegister(long thisRegister, long bytesToRead)
{
}
void writeRegister(word thisRegister, word thisValue)
{
}

Related

NRF24L01 modules not connecting - hardware or coding issue?

I am trying to transmit data from an ultrasonic sensor to an LCD screen using two NRF24L01 modules connected to two separate Arduino UNOs for a distance sensor for a bike. As I am relatively new to Arduino and initially did not know how to use the NRF24L01 module, I followed a tutorial from https://www.electroniclinic.com/nrf24l01-multiple-transmitters-and-single-receiver-for-sensor-monitoring-using-arduino/
, implemented it into my code, and it is not working. I ensured on multiple occasions that the wiring for the NRF24L01 is correct for the Arduino UNOs and attempted to change radio channels, and no use. For others from the website it appeared to be working, but for me it is not. Both programs compile just fine. Is there an issue with my code or is it possible that my NRF24L01 is nonfunctional?
Here is my code:
Transmitter:
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(5, 4); // radio module, pins 5,4 for (CE, CSN)
RF24Network network(radio);
const uint16_t this_node = 01; //address of arduino
const uint16_t node00 = 00;
//setting up pins for each device
const int buzzerPin = 10;
const int aheadTrigPin = 9;
const int aheadEchoPin = 8;
//initialize variables for ultrasonic sensor and data
unsigned long data[3];
unsigned long aheadDistance;
unsigned long aheadDuration;
void setup() {
Serial.begin(9600);
SPI.begin();
radio.begin();
network.begin(69, this_node);
radio.setDataRate(RF24_2MBPS);
pinMode(buzzerPin, OUTPUT);
pinMode(aheadTrigPin, OUTPUT);
pinMode(aheadEchoPin, INPUT);
}
void loop() {
//clears the "trig" condition for ultrasonic sensor
digitalWrite(aheadTrigPin, LOW);
delayMicroseconds(2);
//releases sound waves from ultrasonic sensor for 10 microseconds
digitalWrite(aheadTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(aheadTrigPin, LOW);
//reads "echo" pin to return sound wave travel time in microseconds
aheadDuration = pulseIn(aheadEchoPin, HIGH);
//calculating distance of ultrasonic sensor, in inches (distance = velocity * time)
aheadDistance = (aheadDuration * 0.034 / 2) * 2.54;
Serial.println(aheadDistance);
//activates buzzer to alert rider if danger values are met
if(aheadDistance <= 100) {
tone(buzzerPin, 255);
}
else {
noTone(buzzerPin);
}
//send data to main component
network.update();
data[0] = aheadDistance;
RF24NetworkHeader header(node00);
bool ok = network.write(header, &data, sizeof(data)); // send data
Serial.println("sent to NRF24 server");
delay(200);
}
The reason I am sending an array as the data is because I am currently working on having another transmitter to send data to a single receiver.
Receiver:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <RF24.h>
#include <SPI.h>
//sets up timer for the LCD display to constantly run
//sets up OLED screen
LiquidCrystal_I2C display(0x27,20,4);
RF24 radio (10, 9); // CE, CSN, defines new RF24 radio
RF24Network network(radio);
const uint64_t this_node = 00;
//data variables that will be collected from other arduinos
unsigned long data[3];
unsigned long aheadDistance;
unsigned long groundDistance;
unsigned long backAheadDistance;
void setup()
{
Serial.begin(9600);
display.init(); // intialize lcd
display.backlight(); //open backlight of lcd
display.clear();
//initialize starting screen
display.setCursor(0, 0);
//set up radio module (reciever)
SPI.begin();
radio.begin();
network.begin(69, this_node);
radio.setDataRate(RF24_2MBPS);
}
//method to update screen with current data values
void displayDistance() {
display.setCursor(0, 0);
display.print("Bike Sensor");
display.setCursor(0, 1);
display.print("Front: ");
display.print(aheadDistance);
display.print(" in");
display.setCursor(0, 2);
display.print("Ground: ");
display.print(groundDistance);
display.print(" in");
display.setCursor(0, 3);
display.print("Back: ");
display.print(backAheadDistance);
display.print(" in");
display.display();
}
void connectionLost() // activates when main component is unable to connect to others
{
display.clear();
display.setCursor(0, 1);
display.print("Connection");
display.setCursor(0, 2);
display.print("Lost");
display.display();
}
void loop()
{
network.update();
while(network.available()) {
RF24NetworkHeader header;
Serial.println("connection found");
network.read(header, &data, sizeof(data));
Serial.println("data recieved");
if(header.from_node == 01) {
backAheadDistance = data[0];
}
if(header.from_node == 02) {
aheadDistance = data[1];
groundDistance = data[2];
}
displayDistance();
delay(100);
}
connectionLost();
Serial.println("no connection found");
delay(200);
}
//updates OLED screen and actually displays it on the module
It’s possible that your nRF24L01 is dead. I’ve had many. Some fail. I recommend those from EByte especially ML01DP5 and ML01SP4. These are good.
Also there’s a little known hardware issue: never let the FIFO buffers remain full for over 4ms. Of course it’s hard to know. So simply flush them often as a precaution. I have fixed my code this way and now it’s robust.
Good luck!
Malcolm

ESP32 serial communucation between 2 of them

I'm trying to do a small project between which involves measuring temperature with an ESP32, send it through a serial connection to another ESP32, and make the latter write on the the Serial connection with my computer. I am using some LEDs for monitoring.
The code for the sender is:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <HardwareSerial.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set Global variables
/////////////////////////////////////////////////////////////////////////////////////
// set the LCD number of columns and rows
#define LED1 32
#define RXD2 16
#define TXD2 17
#define DHTPIN 33 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
/////////////////////////////////////////////////////////////////////////////////////
// Serial Communication
HardwareSerial Sender(2); // use Sender
/////////////////////////////////////////////////////////////////////////////////////
DHT dht(DHTPIN, DHTTYPE);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup function
void setup() {
Wire.begin();
Sender.begin(19200,SERIAL_8N1, RXD2, TXD2);
lcd.init();
lcd.backlight();
lcd.print("Finished loading");
pinMode(LED1, OUTPUT);
dht.begin();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loop function
void loop() {
float t1 = dht.readTemperature();
float h = dht.readHumidity();
Sender.print(String(t1));
lcd.setCursor(0,0);
lcd.print("Temp1(C): " + String(t1) + "\n");
lcd.setCursor(0,1);
lcd.print("Humid: " + String(h));
//lcd.print("Temp2(C): " + String(t2) + "\n");
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);
}
The code for the receiver is:
#include <HardwareSerial.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set Global variables
/////////////////////////////////////////////////////////////////////////////////////
// set the Serial Port Data
#define RXD2 16
#define TXD2 17
HardwareSerial Receiver(2); // use Receiver
/////////////////////////////////////////////////////////////////////////////////////
// set other relevant variables
#define LED 33
String message;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup function
void setup() {
// Setup the Serial communication
Receiver.begin(19200,SERIAL_8N1, RXD2, TXD2);
// LED check
pinMode(LED,OUTPUT);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED,LOW);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loop function
void loop() {
while(Receiver.available())
{
message = Receiver.read();
Serial.println("Message received: " + message);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED,LOW);
delay(200);
}
}
I checked that the sensor is correctly measuring and changing, but the receiver is not receiving correctly the information, or at least it isn't able to send it to the Serial connection.
By initializing the communication with the computer with another HardwareSerial instance instead of the default Serial instance the message was sent.

Set up loop keeps looping (Adurino)

I am currently troubleshooting a code in Arduino for a temperature and humidity project. There is a line in the void setup(), Serial.println("Feather LoRa TX Test!");, which keeps popping up. My ideal code is to run that particular line once in the output and that will be it. However, the current code keeps repeating that line again, and again. May I know how do I rectify this issue (The whole code is below)? Thanks in advance!!
#include <RH_RF95.h>
#include <DHT.h>
#define DHTPIN 7 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 3
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
int node = 3; // to change based on node deployment
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
Serial.begin(9600);
delay(100);
Serial.println("Feather LoRa TX Test!");
digitalWrite(RFM95_RST, LOW);
delay(100);
digitalWrite(RFM95_RST, HIGH);
delay(100);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
dht.begin();
rf95.setTxPower(23, false);
}
void loop()
{
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
delay(1000);
return;
}
String d = "{\"Node\":"+ String (node) +",";
d += "\"Temp\":"+ String (t)+",";
d += "\"Hum\":"+ String (h);
d += "} "; // Add a trailing space is necessary
Serial.println("Transmitting...");
char data[d.length()];
d.toCharArray(data,d.length());
Serial.println(d);
rf95.send((uint8_t*)data, sizeof(data));
Serial.println("Waiting for packet to complete...");
delay(1000);
rf95.waitPacketSent();
Serial.println(" complete...");
delay(57000); // delay 1 minute
}
You have an infinite loop before you do any initialization. This will be detected because Arduino have a watchdog timer, and the system will reset.
And on reset setup is called again, and you again enter the infinite loop.
The loop it's about:
while (!Serial);
You must call Serial.begin(...) before that loop:
Serial.begin(9600);
while (!Serial);
Something is resetting your MCU before the code reaches the loop function. Therefore the setup function gets executed again and again. You can add more print messages in between lines so you'll know where it breaks.

Arduino: How to show Serial.print value to any textbox or Notepad

So I saw an Arduino program that will show an RFID value to the serial monitor, I just want to know how can I also show it to any textbox or Notepad or wherever. For example, I opened a Notepad or Word, when I swiped the RFID keychain, it suppose to show the value there. Here's the code I got:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(7, OUTPUT);
}
void loop() {
RfidScan();
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? "0" : ""); // The value I want to show
Serial.print(buffer[i], HEX);
}
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
delay(1000);
Serial.print("\n");
}
void RfidScan()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
}
You can use stty.
Here's a sample :
stty -F /dev/my_serial_port <baud_rate> cs8 cread clocal > filename
Here you redirect all the output from the serial port to a file. To print out the contents as they come in you can use
tail -f filename
There are multiple ways to do this .using a 3rd party tool will be easy to get the serial output. or else you can try following link. I have explained what you need to do.
Try This
Follow this link
What you need is PrintWriter.
Import PrintWriter :
PrintWriter output;
Instantiate output object in the setup() method :
output = createWriter( "data.txt" );
Writes the data to text file
output.println( value );
This "value" data can be retrieved from serial
String value = serial.readString();
Finally flush the data and close the file.
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file

Pyserial doesn't respond to Arduino

I am trying to make USB serial communication between Arduino and Raspberry Pi 3. However, the Arduino IDE serial monitor outputs correct values, but it does not work finely in code using pyserial. The program is running normally, but there is no output on the output terminal.
Arduino code:
#include <DHT11.h>
int pin=2;
int pin1=4;
DHT11 dht11(pin);
DHT11 dht11(pin1);
void setup()
{
Serial.begin(9600);
while(!Serial) {
; //wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
int sensorValue=analogRead(A0);
float outvoltage = sensorValue * (5.0 / 1023.0);
int Level = 6*outvoltage; //The level of wind speed is proportional to the output voltage.
int err1, err2;
float temp1, temp2, humi1, humi2;
if((err1=dht11.read(humi1, temp1))==0&&(err2=dht12.read(humi2, temp2))==0)
{
Serial.print(Level);
Serial.print(" ");
Serial.print(temp1);
Serial.print(" ");
Serial.print(humi1);
Serial.print(" ");
Serial.print(temp2);
Serial.print(" ");
Serial.print(humi2);
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err1);
Serial.print(err2);
Serial.println();
}
delay(1000); //delay for reread
}
Python code:
import serial
ser=serial.Serial("/dev/cu.usbmodemFD1411",9600)
while True:
print ser.readline()