MCUFRIEND_kbv Library for Uno 2.4, 2.8, 3.5, 3.6, 3.95 inch mcufriend Shields

The GFX Library only supports filled circles.
It is easy enough to read the GFX code and adapt it for yourself.

An ellipse is only a circle with two different radius. I would write a universal ellipse function. Circle is just the special case.

You can Google for ellipse, pie-slice and arc. Add your own library functions.

Personally, I reckon that GFX has a useful range of graphics methods.
If you make a library too clever, users never know where to start.
Since many hardware libraries extend GFX, it does make programs easy to port from OLED, GLCD, TFT, ...

David.

Ok, thank you very much

Hi there,
I've got a 2.8" touchscreen. Before using the MCUFRIEND_kbv library, I couldn't get it to work at all, but since using it, I have been very pleased to get it to start to work.

However, the touchscreen is apparently 'mirrored' down the middle, so that with the display in a portrait position, when I move the stylus up the middle (when using Adafruit tftpaint sketch but with MCUFRIEND_kbv library as opposed to the Adafruit_TFTLCD library), it draws a line downwards.

I have tried to resolve this in 2 ways:

-In the sketch I have tried to change the pin configuration:
The default is:
#define YP A3
#define XM A2
#define YM 9
#define XP 8
producing the result as described above.

Modifying it:
#define YP A2
#define XM A3
#define YM 8
#define XP 9
moving the stylus horizontally across the display, produces a vertical line and vice versa!

Defining pins A1, A2, 6 and 7 means that the touchscreen does not work at all, as I have seen this does work for other users.

I have also tried the default...
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

...changing to:
pinMode(XP, OUTPUT);
pinMode(YM, OUTPUT);
but that doesn't work either.

When I run the tftpaint sketch, the serial monitor says that I am using the ILI9341 driver, but the hardware documentation I have for the touchscreen says that it uses a UC8230s driver, so I am really rather bamboozled!

Sorry its such a long post; I'd be interested to know if anybody else has had any similar issues and have managed to get it working :slight_smile:

All the best,
LiFu99

I finally got around to using my UNO, purchased a 3.5 LCD, mcufriends.com and then found your library. After screwing things up I re-learned how to remove the 2.9.3 that showed up after I was 'upgraded' and everything broke. Using Arduino 1.8.4 I used the library manager to install mcufriend_kbv and then the examples started working and displaying correctly. I ran the diagnose_Touchpins and was able to see a change when I move my finger and reloaded. But:

C:\Users\Kevin\Documents\Arduino\libraries\MCUFRIEND_kbv\examples\UTouch_Calibr_kbv\UTouch_Calibr_kbv.ino:29:25: fatal error: TouchScreen.h: No such file or directory

#include <TouchScreen.h>

After searching I could not find the TouchScreen.h anywhere, in the Git zip or from the Arduino library folder.

Thanks for the hard work on this stuff, I hope www.mcsfriend.com treats you right cause I sure want to rip them a new one over the complete lack of documentation. My 3.5 LCD controller is ID:5310 NT35310 whatever that is.

From distant memory, you have to disassemble that shield and correct the wiring.
If your one worked out of the box, this means that the Chinese vendor has fixed the hardware now.

Also from memory: TouchScreen.h is not available through the Library Manager. Google for it. Download the ZIP. Use the Manager to install from ZIP.

I thought that v2.9.3 had the new file layout i.e. utility/ and extras/
So the Manager should Upgrade just fine.

It is useful to hear from punters. If v2.9.3 did not upgrade painlessly, I would like to know.

David.

I have just made a v2.9.7 Release of the library. Please update with the Library Manager.

Nothing changes for existing controllers or regular Arduinos.

Added ILI9225
Fixed R61505U
Fixed ILI9340
UTFTGLUE: Fix setcursor when printing String

Runs on ESP32 now.
Runs on LogicGreen LGT8F328D, LGT8F328P

New sketch to diagnose pins and calibrate TouchScreen.
New sketch to diagnose whether an unusual controller is supported.

David.

Hi David
I just had my mcufriend 3.5 in for UNO worked on MEGA this morning.
The problem is whenever the text needs to be updated on screen, the new text just overlays on top op the old text at the same spot.
I have to overwrite it with spaces to make it work but refresh is very slow.

Here is my code:

float aa1 = random(-15, 45); // current reading
float aa2 = random(-15, 45); // min reading
float aa3 = random(-15, 45); //max reading
tft.setTextSize(2);
int y;
for ( y = 0; y < 7; y++) {

float READINGS[3] = {aa1, aa2, aa3};

for (int i = 0; i < 3; i++)
{

if (READINGS < 0)

  • {*
  • tft.setTextColor(CYAN, BLACK);*
  • }*
    else if (READINGS >= 0 && READINGS < 15)
    * {*
    * tft.setTextColor(BLUE, BLACK);*
    * }*
    else if (READINGS >= 15 && READINGS < 30)
    * {*
    * tft.setTextColor(GREEN, BLACK);*
    * }*
    else if (READINGS >= 30 && READINGS < 35)
    * {*
    * tft.setTextColor(YELLOW, BLACK);*
    * }*
    _ else if (READINGS >= 35)
    * {
    tft.setTextColor(RED, BLACK);
    }
    String empty;
    if ((String(READINGS, 1)).length() == 3 )
    {
    empty = " ";
    }
    else if ((String(READINGS, 1)).length() == 4 && READINGS < 0 )
    {
    empty = " ";
    }
    else {
    empty = "";
    }
    if (READINGS < 0 )
    {
    tft.setCursor(115 + i * SPACE_HOR - OFFSET, Y_INPUTS + y * SPACE_INPUTS);
    tft.println(String(READINGS, 1) + empty);
    }
    else*

    * {
    tft.setCursor(115 + i * SPACE_HOR - OFFSET, Y_INPUTS + y * SPACE_INPUTS);
    // tft.setTextColor(YELLOW, BLACK);
    tft.println( " " + String(READINGS, 1) + empty);
    }
    }
    }*_

Please post a complete sketch. i.e. one that I or anyone else could build.
Either paste it into a CODE window or attach the file.

The snippet that you have posted makes no sense to me. So I can't even guess.

David.

Sorry when I pasted first time here, something went missing.
here is the code.

its a greenhouse monitoring system based on 7 sensor inputs

test.ino (3.04 KB)

I built your sketch. And ran it on a 320x480 TFT plugged into a Uno.

It updates pretty quickly. I had to add a delay to see what was happening.

You seem to be making everything very long-winded.
It appears that you want to format the temperatures neatly, and use different colours for each temperature range.

This could all be done in about 4 or 5 lines.

I don't see why you are worried about time. Your GreenHouse is going to change temperature in minutes rather than milliseconds. If you were monitoring a Nuclear Bomb, things would work faster.

David.

thank you for taking care of my concerns David

My concern is the new text will lay over on top of the old text and make it unreadable after 3-4 updates. it feels like the background is not cleaned up.

how to update the new text with fresh background.

I only learned Arduino for week or two. I know the efficiency of the code is terrible as I am still learning.

Your program works. But -4.0 will line up differently to -14.0

This is another way to do things:

// All the mcufriend.com UNO shields have the same pinout.
// i.e. control pins A0-A4.  Data D2-D9.  microSD D10-D13.
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

int SPACE_HOR = 70;//space between sensor inputs;
int SPACE_VER = 25;// space between each sensor inputs;
int OFFSET = 10;// offset if "-" appears, below zero
int Y_INPUTS = 40; // first sensor starting point(y)
String empty;

void setup(void)
{
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    tft.begin(ID);
    tft.fillScreen(BLACK);
}

void loop(void) 
{
    // for testing purpose
    float aa1 = random(-15, 45); // current reading
    float aa2 = random(-15, 45); // min reading
    float aa3 = random(-15, 45); //max reading
    tft.setTextSize(2);
    int y;
    float READINGS[3] = {aa1, aa2, aa3};

    // system has seven temperature sensors,
    for (  y = 0; y < 7; y++) {
        for (int i = 0; i < 3; i++) {
            tft.setCursor(105 + SPACE_HOR * i, 240 + SPACE_VER * y);
            print_temp(READINGS[i]);
        }
    }
    delay(1000);
}

void print_temp(float temp)
{
    uint16_t colors[] = {CYAN, BLUE, GREEN, YELLOW, RED }, color;
    float range[] = { -15.0, 0.0, 15.0, 30.0, 35.0 };
    for (int i = 0; i < 5 && temp >= range[i]; i++) color = colors[i];
    tft.setTextColor(color, BLACK);
    tft.print(dtostrf(temp, 5, 1, "-##.#"));  //5 char width
}

Note that the colours and ranges are easier to maintain in arrays.
And dtostrf() makes formatting a lot more straightforward than String functions.

Regarding overwriting digits. You have already used tft.setTextColor(color, BLACK)
So it draws a BLACK background instead of transparent. This is the standard way to draw background.

Regarding speed. All AVRs will perform the f-p math at the same speed. A Uno will write to the TFT faster than a Mega because the Shield is mapped nicely to the PORT pins.

David.

Many many thanks to you David. it works way better than mine.
You saved me the day.

Hello!

I bought a 3.5 inch MCUFRIEND display, ID=0x1581.

I tried an example graphictest_kbv and works fine.
The most interesting thing for me is how to display icons.
I found out that should use the icons.c file.
My question is: how to convert my BMP icon into *.c file and how to properly use it?
What is the biggest allowable size of the icon for UNO and MEGA2560?

The next thing is, I tried to use showBMP_kbv_as7.ino to display BMP but with no results, to show images from SD card. Only blue screen.
Then I found a library: MCUFRIEND_kbv_master and the application showBMP_UNO works.
I want to figure out why this scetch works, the other not?
And I found that to generate BMP it needs app 5 seconds....
I searched on internet and find that instead BMP I should use RAW images, not need time for conversion.
How can I convert BMP into RAW and which library I must use for this. Maybe any working example for
LCD with ID=0x1581?
Can someone tell me which controller is this ID 0x1581? Maybe some ILI...?
Thank you in advance for your answers and help and best regards!

  1. Install/Upgrade to the current Release with the Library Manager.
  2. the how_to file explains which controller you have got. (Renesas R61581)
  3. Icon images must be smaller than 32768 bytes.
  4. if you want bigger ones, you will have to split the data into several arrays and show each rectangular section.
  5. there are many programs for converting BMP to RAW. Note that the pushColors() method can do hi-lo and lo-hi data.

The explanation of kbv_as7.ino comes down to third party SdFat.h libraries.

David.

David, thank you for your fast reply.

1.) I install/upgrade MCUFRIENDS library, version 2.9.6 is now installed (it was before too, but I did it again)
2.) I found explanation in file you mentioned that ID 0x1581 means R61581 driver chip, thank you
3.) I againa try to run scetch showBMP_kbv_as7, but I got only a blue screen... Are there any special settings in SdFat library? Unfortunately I am not familliar with libraries. Are this settings in.h or in .cpp file?
4.) I would kindly ask you to give me a tip which BMP to *.c you use to create icons.h. I am confusig regarding that, becouse I found two softwares but both generates 16 bit. If i open and look into icons.h I can see 8 bit (like 0xff, 0xe1, the softwares I found generate 0xffff, 0x1Ae1).
5.) I am trying to find a working example for mcufriend LCD for displaying RAW bitmaps but I can not find. Would you like me to put any direction where/how to find an example?.
6.) I looked into SdFat folder to find any document regading the kbv_as7.ino explanation but I can not find (sorry I am a beginner and I do not understand imediately).

The current Library version is v2.9.7

Have you set the correct directory for your BMP files. I think the kbv_as7 sketch expects them in /bitmaps/ and not the root directory /

Look at the graphictest_kbv sketch. You can change the icon fom penguin to wifi icon.
The wifi icon uses a different endian-ness.

This means that you can render a Raw bitmap that is in 16-bit or 8-bit. It simply swaps the byte order.

I am sure that there are several utilities that produce the C code for an array. Just make sure that you give it the correct attributes. i.e. PROGMEM and const.

David.

Thank you David for your answer.

I found a library version 2.9.7 and installed it.

Now I will focus only to make my own icon.

I modified a file icons.h located in the folder of sketch.
I simply add and rename penguin icon code into myicon
After asembling I got a message " Unidentified reference to myicon"
I am thinking where the icons.h file must be located to include into my sketch?
Becouse my sketch is not in Arduino/examples/....
I modified lines in sketch:
extern const uint8_t myicon[];
tft.pushColors(myicon, 1600, 1);

Edit:
I found out!
Restart of Arduino IDE helps.... :slight_smile:

The easiest way to manage big data arrays is in a separate "Tab".
Look at the graphictest_kbv example. The executable code is in an INO file. The icon data is in a C file. It could be in a CPP file or even an INO file.

Note that if it is in a C file, you have to declare as extern "C"
If it is in a CPP or INO file you just need extern.

extern const uint8_t myicon[];   //1600 pixels in 3200 bytes
tft.pushColors(myicon, 40 * 40, 1);   //pushColors counts in pixels.  icon is 40x40=1600

Obviously a different shape icon would be a different size. The wifi icon is 32x32 and the bytes are stored in a different order to the penguin.

If your PC utility created uint16_t data, you would make it look like uint8_t:

extern const uint16_t PC_icon[];   //1600 pixels in 1600 words
tft.pushColors((const uint8_t *)PC_icon, 40 * 40, 1);   //pushColors counts in pixels.  icon is 40x40=1600

This gets a little fiddly. But it does mean you can cope with big-endian and little-endian data.

David.

Hi, David.
Thank you for your help.
It is a little funny. Yesterday evening I found out and saw a new TAB, left at graphictest_kbv tab....
After that I realize that I can edit icons.c file direct in Arduino IDE, not need to open separately in other software....

Now I am trying to make my first icon.
I managed to convert one small icon and programmed my Arduino. But unfortunately I do not get nice picture. It is a mess of colours and pixels. The same experience I have with wi fi icon, already in icons.c file.
penguin and icon 40_40 works good.
Best regards.