Part 2 – Water Meter – AI code development

To see what happens I decided to give an AI a shot at assisting me with the code development. The chosen AI this time was Gemini from google (Other code writing AI’s are available).

What had to do first was write down what I wanted the AI to produce, what IDE (Arduino) I was using, the hardware configurations, boards and accessories and how they are wired together.


Example question to AI – “I have a ESP32-C6-Touch-AMOLED-1.32 board that I am using the Arduino IDE to program, can you create a simple test program to see if the display is working”

Resulting in the code :-

#include <Arduino_GFX_Library.hpp>

/* Pin Definitions for ESP32-C6-Touch-AMOLED-1.32 */
#define TFT_BL 32
#define TFT_RES 21

/* Create the data bus (QSPI) */
Arduino_DataBus *bus = new Arduino_ESP32QSPI(
    25 /* cs */, 26 /* sck */, 27 /* d0 */, 28 /* d1 */, 29 /* d2 */, 30 /* d3 */);

/* Initialize the SH8601 AMOLED Display */
Arduino_GFX *gfx = new Arduino_SH8601(
    bus, TFT_RES, 0 /* rotation */, true /* IPS */, 
    466 /* width */, 466 /* height */, 
    0 /* col_offset1 */, 0 /* row_offset1 */, 
    0 /* col_offset2 */, 0 /* row_offset2 */);

void setup() {
  Serial.begin(115200);

  // Turn on backlight (if applicable)
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);

  if (!gfx->begin()) {
    Serial.println("gfx->begin() failed!");
  }

  // Initial Screen Clear
  gfx->fillScreen(BLACK);
  delay(500);
}

void loop() {
  // Test Colors
  gfx->fillScreen(RED);
  delay(500);
  gfx->fillScreen(GREEN);
  delay(500);
  gfx->fillScreen(BLUE);
  delay(500);

  // Text Test
  gfx->fillScreen(BLACK);
  gfx->setCursor(80, 220);
  gfx->setTextColor(WHITE);
  gfx->setTextSize(4);
  gfx->println("ESP32-C6");
  gfx->setCursor(80, 270);
  gfx->setTextSize(2);
  gfx->println("Display Working!");
  
  delay(3000);
}

This example use the wrong driver chip type for the display it had used the SH8601 instead of the GC9A01, after finding this and another few attempts we had the screen working and we could write things on the display.

After getting the display working, I asked it count the pulses from the flow sensor, and using my best guess of 450 pulses per litre, calculate the amount of water that had passed since it was powered on.

Next I added a button to GP2 (pin 7), this for testing was a cheap push button, the other side of which was connected to GND (pin 20), this was used to reset the volume of water in the tank available back to full, but to make is hard to do by accidentally you needed to press it five time with less that a second between presses.

As you can see the connector are very small the pitch between sockets is just 1.27mm, much smaller than the normal 2.54mm pitch I am used to. This becomes a problem later as finding a connector to fit this, but at the moment I am pushing single core wire directly into it.

Restarting with a new board.

While the original board was working I really wished I had got a touch screen, so I decided to change the display to a Wave Share ESP32-S3-Touch-LCD-2, this has advantage of a larger 2 inch display with a resolution of 240×320, by favourite features was the touch display and the larger 2.54mm spaced pins on the back.

ESP32-S3-Touch-LCD-2
ESP32-S3-Touch-LCD-2 Front
ESP32-S3-Touch-LCD-2 back
ESP32-S3-Touch-LCD-2 back

Now with a better display and a touch screen, I started experimenting with the AI development and I learned a lot about using the free level AI for this type of development.

  1. Only make one update at a time, only ask the AI to make a single up date, don’t be to ambitious and try to add to much at once, it may work, but my experience is that it will not always end well and you may find you have lost features to expected to remain.
  2. TEST, test and test again, after each update make sure it does what you expect and that you sill have what you had before the update still working.
  3. If you get compile time errors, feed then back to the AI, it will probably fix them without a problem.
  4. Circles (going around in), early on when getting things like the display working, it recommended libraries I did not have installed, these can be difficult to get working if they are not designed or modified for your board. I have been around a few hours of created some code, inputting back in the compile errors, to have the AI confidently tell me they next iteration would fix all the problems, only to find a few steps later we where back with the same setting that had failed in the first place – What I tried and it worked for me was to find an example for the board I am using and feed that code into the AI as a working example of say the display working and ask it to try the same methods.

What I did find if I was patient and will to try things and maybe have to revert back to a working version if needed, this method saved be 1000’s of hours of development time and helped me produce a working product, sometimes I had to study the code it produced, but most of the time, you can get it to fix a problem once you understand what could be going wrong.

Part 3 – How to operate the system the user guide

One Reply to “Part 2 – Water Meter – AI code development”

Comments are closed.