Category Archives: Arduino

Problems with ESP32 configures as both Client and AP

In one project I had an ESP32 configured both as Access Point (AP) and as a Wifi Client. Everything was fine, until I changed my home Wifi password.

After changing the home Wifi password I wanted to change the Wifi password set on the device through the Web UI I made and hosted on the device. The problem started there. I was unable to access the UI.

Reviewing the code realised that the Wifi Client is continuously trying to re-establish the connection in a loop. And hence is unable to process the incoming request and process the page.

So either need to stop the scanning from time to time or stop the scanning when an AP client is connected. For short and fast resolution I stopped the wifi connection retry when a client is connected to AP.

WiFi.softAPgetStationNum()

FASTLed Library and RGB Led

Recently I was trying to make a device that will indicate different status through different light colors.

Now after developing everything a strange thing started to happen – the RGB LED was not showing the colors properly. For example – suppose the current color is Green and then I put Red. It is not glowing pure RED, the Green pixel (the individual green pixel of the LED) is still faintly on.

After some thoughts and searches found the solution is to first turn of the LED fully

leds[0] = CRGB::Black;
FastLED.show();

and then show the new color.

Hope this helps someone.

 

Unsigned Int, Mathematical operations and pitfalls in Arduino or C

Let’s consider the below code:


unsigned long startTime = 1940, stopTime = 0;

if( (stopTime - startTime) > 5000ul)
{
Serial.println("Hello World!");
}

Will this say Hello World?

Yes, it will. If this sounds new or surprising to you, please continue reading below.

Note the variables have been defined as unsigned long. So the result of mathematical operations will also be in unsigned value. Hence though the result of the operation apparently looks to be -1940 but in practise it will be 4294965356

And thus the comparison in the if() will evaluate to true.

Humidity sensor

There are many Humidity sensors available. DHT-11/DHT-20/DHT-22 from Aosong are the cheapest and easily available in India.

DHT-11 is the cheapest. DHT-22 or AM2302 is a good choice for hobby purposes or college projects. DHT20 has replaced DHT22 but still DHT22 is still the most available one.

DHT20 is colored black and DHT22 is white.

DHt-20 DHT-22

There are many manufacturers, even nameless ones.  But sensor from Aosong is the preferred.

The module needs a pullup resistor. 10K will do. They can be put at a distance from the controller. The length of the cable depends on the voltage. With 5v it can be upto 3mts and for 3.3v the max is 1 meter.

Best library in my opinion is the one from Adafruit, which is available here.

The library can be downloaded from here also DHT-sensor-library by Adafruit

Some other Humidity sensors are SHT20D, HTU21D-F, BME280. The HTU21D-F has protective cover or membrane on the sensor. SHT2x or SHT3x also has protective caps available.

ESP-01 Serial.println() or Serial.print() not working

I went crazy, mad trying to understand why the module is not printing anything to the Serial Monitor or receiving any AT commands.

I went through lots of articles on the internet and was on the verge of throwing the module away. Just that my mind was not willing to believe that it is dead – because I was able to burn OS / Firmware to the module every time I tried and the status showed success. And my suspicion was confirmed accidentally – I put some Serial.print code in loop instead of setup and it did throw output to serial monitor. It confirmed that something is wrong with the settings of Arduino IDE.

After lots of tweaking and trials I found that – the “Built In Led” settings of Arduino that mattered.  The built in LED is on the TX line of the module, hence just after printing some output when I toggled the LED, the print function didn’t get enough time to write to the monitor. To solve the matter a delay can be used also.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("");
  Serial.println("Hello");
  delay(1000);
  pinMode(1, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(1, LOW); // turn the LED on (HIGH is the voltage     level)
  delay(500); // wait for a second
  digitalWrite(1, HIGH); // turn the LED off by making the voltage LOW
  delay(300); 
  digitalWrite(1, LOW); 
  delay(500); // wait for a second
  digitalWrite(1, HIGH); 
  delay(2000); 
}

In my module the LED is on PIN 1.

ESP-01 and ESP-01S Burning AT ROM- New and Updated

Few years back I had posted an article on burning ROM on ESP-01. The method worked good on my older boards but not working on the newer chips.

I needed to spend quite some hours to figure out the problem. And then it turned out that it is the SPI mode that is the matter. Also the new Flash tool (v 3.9.2 ) is not robust. It often becomes unresponsive.

  • Upload a blank sketch to Arduino Uno
  • The new ones need DOUT, 26 Mhz Xtal (check the board), SPI Speed 40 Mhz, 8Mbit. Burning with other modes may result in success but the chip will not work and will end up with boot errors. I got reset reason: 2, boot(3,7)
  • Connect RX to RX and TX to TX
  • GPIO_0 will go to GND
  • VCC and CH_PD will go to the 3.3v supply line
  • Select the appropriate ESP8266 board from Tools > Board menu.
  • Select the appropriate Flash mode (Only DOUT works for new chips. Old chips work with both QIO and DOUT)
  • Select baudrate as 115200
  • Start upload
  • RESET the module. This is a critical step and the timing matters. It may need a few tries to get it perfect.

The new Flash tool (v 3.9.2 ) is not robust. It often becomes unresponsive. The quick fix solution I found is pressing enter in the console (black window that it opens) and then disconnecting and reconnecting the Arduino (USB cable).

Wrong flashing may cause boot problems. To correct ERASE and FLASH OR FLASH all memory locations like shown below

The appropriate addresses can be found in the folder \ESP8266_AT_Bin_V1.6\bin\at\README.md

Please note – DoNotChgBin is unchecked.

After flashing Firmware version 1.6

AT+GMR
AT version:1.6.0.0(Feb 3 2018 12:00:06)
SDK version:2.2.0(f28eaf2)
compile time:Feb 6 2018 14:36:23
Bin version(Wroom 02):1.6.0
OK

All diagnostic messages  are sent at 74880 Bauds.
But AT commands are available only at 115200 Bauds.

After flashing disconnect GPIO_0 from GND and RESET

On proper flashing it will show ready like below

 

Here is the latest flash tool
Here is the ROM Version 1.6
Here is the ROM Version 1.6.2

NOTES:

  • Adequate power supply is needed for burning. I am using a buck convertor capable of delivering upto 1 amp.
  • Burning new chips with QIO will result in a successful burning but the code will not run.

 

 

Arduino and memory pointers

Arduino and memory pointers

I needed to use pointers in my Arduino code. The pointer was for a string. So it was a pointer to an array or pointer to a pointer. I had last worked with pointers many decades back, so it needed a bit of remembering and experimenting to get it right. The problem was a little more compounded as it was a  pointer to an array. On the internet there are many documents but none of them actually helped. Finally I had to refer to old textbooks and college materials. Here is a brief for quick reference and recalling pointers.

In classic C strings are arrays. It can be also understood from the declaration of strings in C  – char str[] = “Hello World”;
Continue reading Arduino and memory pointers

Websites for buying electronics parts in India

Amazon.in – exorbitant prices. Often 3 or 4 times. There are sellers who will send wrong products. Amazon will initiate a replacement which has little chance of happening if the price of item is low.

Calcuttaelectronics.com — very reliable, parts quality good. But since 2020 (after the first lockdown) their variety and stock has gone down and as of date of writing many parts are “out of stock” for many weeks. Good packaging. Support was a little disappointing.

Electroncomponents.com — has a good collection and very cheap. But the price and variety made me a bit curious (and cautious), so checked the internet and found they had a very bad reputation in the recent past. But still I ordered and got my items in time.
Parts quality mixed – some are of good quality some are cheap ones. Packaging okay. Delivery time is good.

Iotwebplanet.com — parts are good, delivery times is good, variety is good, but orders may get cancelled silently. Has COD facility. Good packaging.

Robomart.com — They are reliable. Collection is okay sort of.  Parts are often good but not always or some parts can be quite cheap quality. Packaging good.

Tanotis.com — this is in a different league from the rest including Amazon. Most parts which are not available in any other websites can be found here. Quality is very good. But the delivery time is long and sometimes order gets cancelled (but they do inform and give refund). Though costly but is still reasonable than other sites in that league. Very good packaging.
Continue reading Websites for buying electronics parts in India