I'm programming an ESP32 through Arduino IDE, and I'm having a strange issue with Arduino's WiFi library. When I connect to my WiFi network, it forces reading of a particular pin (Pin 2) to 4095. With the line of code connecting to WiFi commented out, I get a correct reading on a sensor I have connected to that pin, but with it included it's stuck at 4095. This also happens when the microcontroller is disconnected from the rest of the circuit. With the line commented out I get a white noise reading, but the 4095 reading with the line included. Here's the code:
//Libraries
#include <WiFi.h>
//Wi-Fi Connection Parameters
const char* ssid = "REMOVED";
const char* password = "REMOVED";
const int sensePin = 2;
//Initialize WiFi Server
WiFiServer server(80);
void setup() {
pinMode(sensePin, INPUT);
int senseOut = 0;
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password); //THIS LINE CAUSES 4095 READING
}
void loop() {
TestSensor();
}
void TestSensor()
{
for (int i = 0; i < 100; i++)
{
senseOut = analogRead(sensePin);
Serial.println(senseOut);
delay(100);
}
}
Example of white noise reading:
1251
1263
1275
1254
1237
1200
1149
1095
1040
976
928
868
835
805
806
820
778
752
819
1002
1516
1675
1687
1693
1659
1674
1702
1713
1727
Any ideas what could be causing this? Thank you.
You need to use another analog input.
These pins do not support analog input when WiFi is in use:
00,
02,
04,
12,
13,
14,
15,
25,
26,
These ones do:
32,
33,
34,
35,
36,
39,
Use one of these last 6 to connect your sensor.
Related
I am trying to get the following code running on an Arduino Leonardo (because it has native usb) and it doesn't work. It doesn't even print anything to the serial monitor. When I try it with an Arduino Nano, everything works fine. I have considered whether maybe it is because the softwareSerial library doesn't work with Leonardo, but I couldn't find any information about it online. Anybody has any idea why it doesn't work?
/*****************************
RFID-powered lockbox
Written by: acavis, 3/31/2015
Modified: Ho YUN "Bobby" Chan # SparkFun Electronics Inc., 11/10/2017
Inspired by & partially adapted from
http://bildr.org/2011/02/rfid-arduino/
Description: This sketch will move a servo when
a trusted tag is read with the
ID-12/ID-20 RFID module
----------HARDWARE HOOKUP----------
PINOUT FOR SERVO MOTOR
Servo Motor ----- Arduino
GND GND
Vcc 5V
SIG D9
PINOUT FOR SPARKFUN RFID USB READER
Arduino ----- RFID module
5V VCC
GND GND
D2 TX
PINOUT FOR SPARKFUN RFID BREAKOUT BOARD
Arduino ----- RFID module
5V VCC
GND GND
GND FORM
D2 D0
Optional: If using the breakout, you can also
put an LED & 330 ohm resistor between
the RFID module's READ pin and GND for
a "card successfully read" indication.
Note: Make sure to GND the FORM pin to enable the ASCII output format.
--------------------------------------------------
******************************/
#include <SoftwareSerial.h>
#include <Servo.h>
// Choose two pins for SoftwareSerial
SoftwareSerial rSerial(2, 3); // RX, TX
// Make a servo object
Servo lockServo;
// Pick a PWM pin to put the servo on
const int servoPin = 9;
// For SparkFun's tags, we will receive 16 bytes on every
// tag read, but throw four away. The 13th space will always
// be 0, since proper strings in Arduino end with 0
// These constants hold the total tag length (tagLen) and
// the length of the part we want to keep (idLen),
// plus the total number of tags we want to check against (kTags)
const int tagLen = 16;
const int idLen = 13;
const int kTags = 4;
// Put your known tags here!
char knownTags[kTags][idLen] = {
"111111111111",
"444444444444",
"555555555555",
"7A005B0FF8D6"
};
// Empty array to hold a freshly scanned tag
char newTag[idLen];
void setup() {
// Starts the hardware and software serial ports
Serial.begin(9600);
rSerial.begin(9600);
// Attaches the servo to the pin
lockServo.attach(servoPin);
// Put servo in locked position
// Note: Value may need to be adjusted
// depending on servo motor
lockServo.write(0);
}
void loop() {
// Counter for the newTag array
int i = 0;
// Variable to hold each byte read from the serial buffer
int readByte;
// Flag so we know when a tag is over
boolean tag = false;
// This makes sure the whole tag is in the serial buffer before
// reading, the Arduino can read faster than the ID module can deliver!
if (rSerial.available() == tagLen) {
tag = true;
}
if (tag == true) {
while (rSerial.available()) {
// Take each byte out of the serial buffer, one at a time
readByte = rSerial.read();
/* This will skip the first byte (2, STX, start of text) and the last three,
ASCII 13, CR/carriage return, ASCII 10, LF/linefeed, and ASCII 3, ETX/end of
text, leaving only the unique part of the tag string. It puts the byte into
the first space in the array, then steps ahead one spot */
if (readByte != 2 && readByte!= 13 && readByte != 10 && readByte != 3) {
newTag[i] = readByte;
i++;
}
// If we see ASCII 3, ETX, the tag is over
if (readByte == 3) {
tag = false;
}
}
}
// don't do anything if the newTag array is full of zeroes
if (strlen(newTag)== 0) {
return;
}
else {
int total = 0;
for (int ct=0; ct < kTags; ct++){
total += checkTag(newTag, knownTags[ct]);
}
// If newTag matched any of the tags
// we checked against, total will be 1
if (total > 0) {
// Put the action of your choice here!
// I'm going to rotate the servo to symbolize unlocking the lockbox
Serial.println("Success!");
lockServo.write(180);
}
else {
// This prints out unknown cards so you can add them to your knownTags as needed
Serial.print("Unknown tag! ");
Serial.print(newTag);
Serial.println();
}
}
// Once newTag has been checked, fill it with zeroes
// to get ready for the next tag read
for (int c=0; c < idLen; c++) {
newTag[c] = 0;
}
}
// This function steps through both newTag and one of the known
// tags. If there is a mismatch anywhere in the tag, it will return 0,
// but if every character in the tag is the same, it returns 1
int checkTag(char nTag[], char oTag[]) {
for (int i = 0; i < idLen; i++) {
if (nTag[i] != oTag[i]) {
return 0;
}
}
return 1;
}
Code is from here: https://learn.sparkfun.com/tutorials/sparkfun-rfid-starter-kit-hookup-guide/all
From: https://docs.arduino.cc/learn/built-in-libraries/software-serial
Not all pins on the Mega and Mega 2560 boards support change
interrupts, so only the following can be used for RX: 10, 11, 12, 13,
14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12
(66), A13 (67), A14 (68), A15 (69). Not all pins on the Leonardo and
Micro boards support change interrupts, so only the following can be
used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
So this SoftwareSerial rSerial(2, 3); // RX, TX won't work.
Also Arduino Leonardo has two hardware serials. I don't see why you would use SoftwareSerial here.
I am trying to implement a webserver using the W1500 shield (I think it's called that) that also links to a 16x2 LCD and some DHT22 sensors. In my setup function, I have Ethernet.begin(mac, ip); but I can only utitlize the lcd.print() function before calling Ethernet.begin(). After calling this, any lcd.print() does not have any effect (even in the loop, the print function does nothing).
What would cause this and how can I get around this?
Thank you!
(This is my first post, I'm bound to have done something wrong so please let me know!)
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <Vector.h>
#include <LiquidCrystal.h>
#define DHTPIN1 6
#define DHTPIN2 7
#define DHTPIN3 8
#define DHTTYPE DHT22
DHT dht[] = {
{DHTPIN1, DHT22},
{DHTPIN2, DHT22},
{DHTPIN3, DHT22},
};
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte ip[] = { 10, 0, 0, 99 };
EthernetServer server(80);
float h[3];
float t[3];
float tf[3];
float avgTempC = 0.00;
float avgTempF = 0.00;
float avgHumid = 0.00;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
// THIS CORRECTLY PRINTS TO LCD
lcd.print("TEMP: HUMID:");
for (auto& sensor : dht) {
sensor.begin();
}
Ethernet.begin(mac, ip);
server.begin( );
// HERE IS WHERE IT DOES NOT WORK
String avgTString = String(avgTempF);
String avgHString = String(avgHumid);
lcd.setCursor(0, 1);
lcd.print(avgTString + "F");
lcd.setCursor(8, 1);
lcd.print(avgHString + "%");
Serial.print("server.begin() called\n");
}
void loop() {...}
You did not mention which ethernet board and which microcontroller board you actually use.
Please check which pins are used by your ethernet shield. Please have a look at the technical description of your ethernet shield.
If you have - for example - an Arduino Ethernet Shield V1, you find the details here:
https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1
Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work.
You cannot use these pins to communicate with the LCD - at least if the LCD does not also use SPI (e.g. if it is connected via 74HC595 shift registers) and even then, you'd have to deselect the other SPI devices and then select the LCD device.
For details, see:
https://www.arduino.cc/en/reference/SPI
I made the necessary connections such as following ;
14 - Vcc
7 - Gnd
1 - A7
2 - Vcc
13 - 6
and the following is the code ;
const int analog_value_Z = A7;
const int enable = 7;
void setup() {
pinMode(enable, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(enable,HIGH);
Serial.print(analogRead(analog_value_Z));
Serial.print("\n");
delay(1000);
digitalWrite(enable,LOW);
Serial.print(analogRead(analog_value_Z));
Serial.print("\n");
delay(1000);
}
Since pin1 of switch is directly connected to analog read pin and the other pin is connected to Vcc, I am trying to switch between ON/OFF states by applying HIGH/LOW to enable pin. I expect result such as ;
1023
0
1023
0
.
.
.
Instead I get :
1023
1023
1023
.
.
What am I doing wrong ? This IC opens and closes the switch between Y and Z pins of it with the change in enable pin. Please provide some guidance.
I am trying to get a temperature reading using a single dsb1820 temperature sensor attached to an esp32 micro controller. The sensor is attached to GPIO-4 of the esp32. I intend to send the temperature reading to a cloud.
The problem i am facing is that the temperature reading always gives the value -127.
I read somewhere online that when the dsb1820 returns -127 it means that the sensor is not connected.
Am I using the wrong pin to connect the sensor?
#include "OneWire.h"
#include "DallasTemperature.h"
#include <WiFi.h>
#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWORD"
OneWire oneWire(4);
DallasTemperature tempSensor(&oneWire);
void setup(void)
{
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
tempSensor.begin();
}
void loop(void)
{
tempSensor.requestTemperaturesByIndex(0);
Serial.print("Temperature: ");
Serial.print(tempSensor.getTempCByIndex(0));
Serial.println(" C");
delay(2000);
}
Check your cables and:
const int oneWireBus = 32; // on pin 32 /GPIO7/D0 on pcb (a 4.7K resistor is necessary)
OneWire oneWire(oneWireBus);
and it should be the middle pin of the sensor (see my graphic)
EDIT
The DevKit has no pin 4 either you use GPIO4 (4 on the pcb) which is in Arduino 24 BUT
The following strapping pins: 0, 2, 4, 5 (HIGH during boot), 12 (LOW
during boot) and 15 (HIGH during boot) are used to put the ESP32 into
bootloader or flashing mode. Don't connect peripherals to those pins!
If you do, you may have trouble trying to upload code, flash or reset
the board.
Connect to 32 (GPIO7 or D0 on the pcb) as this is safe for testing
If you have that wrong or no/wrong resistor it will give you -127 (or you killed the sensor/it was DOA).
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).