This sketch works on Arduino Uno but does not to work on Arduino Mega 2560. The same connections. The same logic konwerter. To be sure I have tried all TX and RX pins on the board 0, 1, 14-21 and I did not find any solution.
So I think the sketch has issue I cant resolve.
Sketch upload is succesful. No errors.
#include <stdio.h>
#include <PMS.h>
#define N 23
char linia1[16], linia2[16];
String sumPM25, sumPM10;
unsigned char bufor [N];
int PM25 = 10, PM10 = 10;
int wartoscPM25(unsigned char *thebuf);
int wartoscPM10(unsigned char *thebuf);
char sprawdzLancuch(unsigned char *thebuf, char leng);
int a=0;
void setup(){
Serial.begin(9600);
}
void loop(){
if(Serial.find(0x42))
Serial.readBytes(bufor,N);
if(bufor[0] == 0x4d){
if(sprawdzLancuch(bufor,N)){
PM25=wartoscPM25(bufor);
PM10=wartoscPM10(bufor);
}
}
sprintf(linia1,"%d",PM25);
Serial.print(linia1);
sprintf(linia2,"%d",PM10);
Serial.println(linia2);
delay(1000);
}
int wartoscPM25(unsigned char *buf)
{
int PM25v;
PM25v=((buf[11]<<8) + buf[12]);
return PM25v;
}
int wartoscPM10(unsigned char *buf)
{
int PM10v;
PM10v=((buf[13]<<8) + buf[14]);
return PM10v;
}
bool sprawdzLancuch(unsigned char *buf, int dlugosc)
{
bool flaga=0;
int suma=0;
for(int i=0; i<(dlugosc-2); i++){
suma+=buf[i];
}
suma=suma + 0x42;
if(suma == ((buf[dlugosc-2]<<8)+buf[dlugosc-1]))
{
suma = 0;
flaga = 1;
}
return flaga;
}
I use logic converter on both Uno and Mega to connecte PMS3003.
The serials are on the following pins.
Serial: 0 (RX) and 1 (TX);
Serial1: 19 (RX) and 18 (TX);
Serial2: 17 (RX) and 16 (TX);
Serial3: 15 (RX) and 14 (TX).
Since Serial is also connected to the USB, you should probably connect the deice up to one of the other ports and then use the corresponding SerialX to communicate to it.
Also verify you have the RX and TX pins between the Arduino and the device (and the logic converters) hooked up correctly.
Sometimes datasheets are show the Rx to a device meaning Rx from the host (Rx into the device), and sometimes Rx to the host (ie TX from the device).
Related
I have an Arduino Nano 33 iot that outputs data via Serial at 38400 baud, connected via USB. Setup starts with Serial.Begin. The Raspberry Pi 4, running Raspian buster is set up to receive the data. It can see the correct port, /dev/ttyACM0, but nothing comes in.
I even installed the correct Arduino IDE and SAMD board package on the Raspberry Pi. It still does not find it until after the IDE uploads the replacement sketch and the CPU is reset. The IDE can grab the serial number and board type though. I can then exit out of the IDE and the Arduino is still pumping out serial to the Raspberry Pi.
The only other way to make it work is by pressing the reset button on the Arduino every time a reboot is done on the Raspberry Pi. Serial was tested on the Pi using screen.
Neither of these options are convenient. What am I missing?
/*
Connects via I2C to a CMPS14, outputs NMEA0183 HDM sentences via Serial (38400 baud)
By James Henderson, 2014, adapted to output NMEA sentences by Ian Van Schaick
*/
#include <Keyboard.h>
#include <Wire.h>
#define CMPS14_ADDRESS 0x60 // Address of CMPS14 shifted right one bit for arduino wire library
#define ANGLE_8 1 // Register to read 8bit angle from
unsigned char high_byte, low_byte, angle8;
signed char pitch, roll;
float angle16;
int fine;
float bearingH; // Holds whole degrees of bearing
float bearingL; // Holds decimal digits of bearing
int bearing;
char nbsp;
char mystring[25];
char mystring2[25];
char mystring3[25];
int software;
int cal;
unsigned int _last_status;
uint8_t checksum(char *s)
{
uint8_t c = 0;
while (*s)
c ^= *s++;
return c;
}
void CMPS14_eraseProfil()
{
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE0);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE5);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE2);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
}
//Correct heading for known deviation
int DeviationCorrect(int Head)
{
return 0;
}
void setup() {
Serial.begin(38400); // Start serial port
Wire.begin();
nbsp = 32;
// CMPS14_eraseProfil();
}
void loop() {
Wire.beginTransmission(CMPS14_ADDRESS); //starts communication with CMPS14
Wire.write(ANGLE_8); //Sends the register we wish to start reading from
Wire.endTransmission();
// Request 5 bytes from the CMPS14
// this will give us the 8 bit bearing,
// both bytes of the 16 bit bearing, pitch and roll
Wire.requestFrom(CMPS14_ADDRESS, 26);
while (Wire.available() < 26); // Wait for all bytes to come back
// software = Wire.read();
// Serial.print("Version: ");
// Serial.println(software);
angle8 = Wire.read(); // Read back the 5 bytes
high_byte = Wire.read();
low_byte = Wire.read();
pitch = Wire.read();
roll = Wire.read();
// int i = 6;
// while (i <= 25) {
// Wire.read();
// i++;
// }
//
// cal = Wire.read();
// Serial.print("Cal: ");
// Serial.println(cal);
bearing = ((high_byte << 8) + low_byte) / 10;
fine = ((high_byte << 8) + low_byte) % 10;
byte data[128] = "$HCHDM,";
data[8] = bearing;
// int deviation = 0;
//DeviationCorrect(bearing);
// bearing = bearing;
//+ deviation;
//Print out NMEA 0183 string HDM
snprintf(mystring, sizeof(mystring), "$HCHDM,%d.%d,M", bearing , fine);
uint8_t crc = checksum(mystring + 1);
Serial.print(mystring);
Serial.print("*");
if (crc < 16) Serial.print("0");
Serial.println(crc, HEX);
//Print out NMEA 0183 string XDR for Pitch
snprintf(mystring2, sizeof(mystring2), "$HCXDR,A,%d,D,PITCH", pitch);
uint8_t crc2 = checksum(mystring2 + 1);
Serial.print(mystring2);
Serial.print("*");
if(crc2 < 16) Serial.print("0");
Serial.println(crc2, HEX);
//Print out NMEA 0183 string XDR for Roll/Heel
snprintf(mystring3, sizeof(mystring3), "$HCXDR,A,%d,D,ROLL", roll);
uint8_t crc3 = checksum(mystring3 + 1);
Serial.print(mystring3);
Serial.print("*");
if(crc3 < 16) Serial.print("0");
Serial.println(crc3, HEX);
delay(100);
}
I'm trying to drive the LTC1664 DAC from an Arduino (Mega 2560). The SPI data and clock coming out of the Arduino is always synched (rise and falls) for all 4 modes when the DAC data sheet indicates phasing
I've tried all modes, the SS line on the chip is being brought low during the writes (as it should) and I've tried different speeds and shiftOut(), setClockDivider(), setDataMode(), beginTransaction() and endTransaction().
Is this a bug in Arduino SPI, something specific to the Mega 2560, should I try an Uno or Due? Help please! Code below o-scope traces.
BTW: The 16 bit word I'm trying to transmit is, 0x3600 (o-scope trace truncated).
/*
Test DAC Control
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
// inslude the SPI library:
#include <SPI.h>
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model);
enum models{LTC1664};
enum models model;
// set pin 10 as the slave select for the digital pot:
const int DAC_SS_Pin = 22;
void setup() {
// set the DAC_SS_Pin as an output:
// initialize SPI:
Serial.begin(115200);
SPI.begin();
Serial.println("SPI.begin");
pinMode(DAC_SS_Pin, OUTPUT);
}
void loop() {
// go through the six channels of the digital pot:
for (int channel = 1; channel < 6; channel++) {
delay(500);
DAC(128*channel, channel, DAC_SS_Pin, LTC1664);
}
delay(1000);
Serial.println("new loop");
}
/*
DAC Control
This function controls an LTC1664 Micropower Quad 10-Bit DAC.
The LTC1664 is SPI-controlled,and to command it, you send one 16 bit word,
A3 A2 A1 A0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 X1 X0
| ADDR | INPUT CODE | DON'T CARE
The circuit:
* CS - to digital pin 22 (SS pin)
* SDI - to digital pin 51 (MOSI pin)
* SDO - to digital pin 50 (MISO pin, not used in this function)
* CLK - to digital pin 52 (SCK pin)
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model) {
// take the SS pin low to select the chip:
digitalWrite(SS_Pin, LOW); delay(100);
unsigned short buf, b16;
unsigned char *c, b; c = (unsigned char *) &buf;
switch (model)
{
case LTC1664:
if (channel > 4) channel = 0xF;
buf = (channel << 12) | ((value & 0x3FF) << 2);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
b16 = SPI.transfer16(buf);
Serial.print("0x" + String(buf, HEX)); Serial.println("\t0x" + String(b16, HEX) + "\t");
SPI.endTransaction();
break;
default:
break;
}
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(SS_Pin, HIGH);
// printf("value = 0x%04x", buf);
}
Turns out it wasn't related to the Arduino SPI function or phasing issues, the LTC1664 has a CLR pin that I had attached to a GPIO but had failed to command HIGH, it had been floating and inhibiting the chip, command HIGH and everything is good now.
I have Arduino Uno(Atmega328p) and external EEPROM connected via I2C.
My code in Arduino Studio:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define EEPROM_ADDR 0x50
#include <M24AA01.h>
M24AA01 dev(0);
byte data[1] = {1};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
writeEEPROM(1);
uint8_t readData = readEEPROM(1);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5,30);
display.print("read:");
display.print(data);
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
}
void writeEEPROM(unsigned int eeaddress)
{
Wire.beginTransmission(EEPROM_ADDR);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data[0]);
Wire.endTransmission();
delay(5);
}
uint8_t readEEPROM(unsigned int eeaddress)
{
uint8_t rdata = 0xFF;
Wire.beginTransmission(EEPROM_ADDR);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_ADDR,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
My physical connection of the EEPROM:
PIN 1: A0 - 5V(VCC)
PIN 2: A1 - 0(GND)
PIN 3: A2 - 0(GND)
PIN 4: VSS - 0(GND)
PIN 5: SDA - SDA
PIN 6: SCL - SCL
PIN 7: WP - 0(GND)
PIN 8: VCC - 5V(VCC)
I have a DIP version of EEPROM.
From the documentation I read that address of EEPROM is 0x50 (1010000). A0 is connected to 5V, but anyway A0,A1,A2 are "dont cares" as per documentation, so I believe address is fine.
No matter what I write to the EEPROM, in this case I just write 1 byte of data (number 1) - it reads 255.
One thing I noticed is that I do not specify in the code if I want to write or read (in documentation the 8th bit is read/write bit, read = 1, write = 0), but I do not know how to use it in code.
I'm building a little car, remote controlled by a Wemos D1 board, in order to set the WiFi connection and the control logic I'm running this script:
#include <ESP8266WiFi.h>
const char* pass = "**********";
const char* ssid = "**********";
IPAddress ip(192,168,1,91);
IPAddress gat(192,168,1,1);
IPAddress dns(192,168,1,1);
IPAddress sub(255,255,255,0);
WiFiServer s(2000);
int inA1 = 1;
int inA2 = 2;
int enA = 3;
int inB1 = 4;
int inB2 = 5;
int enB = 6;
int trigger = 7;
int echo = 8;
double vSuono = 343; //Unità di misura: m/s
int speed = 255;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.config(ip,gat,sub,dns);
WiFi.begin(ssid,pass);
delay(500);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.println("Connected!");
delay(30);
s.begin();
Serial.println("Server running!");
delay(30);
//Here starts the problems
pinMode(inA1,OUTPUT);
pinMode(inA2,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(inB1,OUTPUT);
pinMode(inB2,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient c = s.available();
delay(30);
if(c){
Serial.println("New client connected!");
delay(3);
while(c.connected()){
if(c.available()){
String command = c.readStringUntil('\n');
if(command == "forward"){
Serial.println("forward");
forward(speed);
}else if(command == "right"){
Serial.println("right");
right(speed);
}else if(command == "left"){
Serial.println("left");
left(speed);
}else{
Serial.println("back");
back(speed);
}
}
delay(30);
}
c.stop();
}
}
void forward(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void left(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void right(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void back(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void stop(){
digitalWrite(inA1,LOW);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,LOW);
}
The problem is that when the board execute the pinMode function in the setup() block, the board stop the execution, crash and restart, and I'm not able to ping the board.
If I comment all the portion of the setup() block, with the pinMode calls, the program starts to work but obviously I can't use the pins.
On the serial monitor when the board crash appears this messages:
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
What could be the problem?
I don't know the pin mapping by heart, but you should stick to the GPIO pins named D1, D2...D8. You've named them 1, 2...8 which are different pins. You likely used a pin which is used by something else (like serial or reset).
int inA1 = D1;
int inA2 = D2;
int enA = D3;
int inB1 = D4;
int inB2 = D5;
int enB = D6;
int trigger = D7;
int echo = D8;
I was getting the same wdt reset error while trying to use pinMode with digitalWrite() function.
I solved it by figuring out how the Wemos Pin mapping works.
Actually, you need to refer the pin in 'Dx' notation.
e.g
digitalWrite(D15,LOW);
or
pinMode(D15, OUTPUT);
Also, make sure to select "Wemos D1 R1" in Tools > Boards so that Dx constants will match
the labels.
Have a look at the conversation here on Arduino FOrum to understand more about wemos pin mapping:
https://forum.arduino.cc/index.php?topic=545113.0
I must send 3 values gave by 3 potentiometers ,connected by an Arduino Uno, and send them to another Arduino Uno whith a serial comunication. The received values must be distributed in 3 servo motors so that each knob able to control the servo motor movement.
the problem with this program is that the values received are not distributed correctly (for example the case that the value of the potentiometer 1 is to be read by the servo motor 3 or other cases). I ask if I could help to synchronize data received with the distribution of them to the servo motors. Thanks in advance.
sketch arduino with potentiometers:
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255); /* synch symbol */
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
sketch arduino with servo motors:
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
if (BTserial.available() > 0) {
if (BTserial.available() == 255) { /* synch */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
master code( no modificated):
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255);
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
slave code( modificated):
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
// Read serial input:
if (BTserial.available() > 0) {
byte synch_symbol = BTserial.read();
if (synch_symbol == 255) {
while (BTserial.available() < 3) { }; /* wait for values */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
/* do something with values */
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}