Advertisement
Guest User

Untitled

a guest
Feb 11th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. /*
  2. This is an example on how to use the 1.8" TFT 128x160 SPI ST7735 display using the Adafruit library.
  3.  
  4. ST7735 TFT SPI display pins for Arduino Uno/Nano:
  5. * LED = 3.3V
  6. * SCK = 13
  7. * SDA = 11
  8. * A0 = 8
  9. * RESET = 9
  10. * CS = 10
  11. * GND = GND
  12. * VCC = 5V
  13.  
  14. Another version marked as KMR-1.8 SPI:
  15. This version only supports 3.3V logic so put a level shifter for all I/O pins, or a 2.2k resistor between
  16. the display and arduino, and a 3.3k resistor to ground to create a simple voltage divider to produce a 3V output.
  17. * LED- = GND
  18. * LED+ = 15Ξ© resistor to 5V
  19. * CS = 10
  20. * SCL = 13
  21. * SDA = 11
  22. * A0 = 8
  23. * RESET = 9
  24. * VCC = 5V or 3.3V (the display has it's own 3.3V regulator)
  25. * GND = GND
  26.  
  27. Hardware SPI Pins:
  28. * Arduino Uno SCK=13, SDA=11
  29. * Arduino Nano SCK=13, SDA=11
  30. * Arduino Due SCK=76, SDA=75
  31. * Arduino Mega SCK=52, SDA=51
  32.  
  33. SPI pin names can be confusing. These are the alternative names for the SPI pins:
  34. MOSI = DIN = R/W = SDO = DI = SI = MTSR = SDA = D1 = SDI
  35. CS = CE = RS = SS
  36. DC = A0 = DO = DOUT = SO = MRST
  37. RESET = RST
  38. SCLK = CLK = E = SCK = SCL = D0
  39.  
  40.  
  41. Libraries needed:
  42. https://github.com/adafruit/Adafruit-ST7735-Library
  43. https://github.com/adafruit/Adafruit-GFX-Library
  44.  
  45.  
  46. Reference page for GFX Library: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-gfx-graphics-library.pdf
  47.  
  48.  
  49. Color is expressed in 16 bit with Hexadecimal value.
  50. To select a particular color, go here and copy the "Hexadecimal 16 bit color depth value":
  51. https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
  52.  
  53. Common colors:
  54. * BLACK 0x0000
  55. * BLUE 0x001F
  56. * RED 0xF800
  57. * GREEN 0x07E0
  58. * CYAN 0x07FF
  59. * MAGENTA 0xF81F
  60. * YELLOW 0xFFE0
  61. * WHITE 0xFFFF
  62.  
  63. A way to select a color is to write: "ST7735_BLACK", or "ST7735_BLUE", etc.
  64. Or just write the code for the color. Either way, it works.
  65.  
  66.  
  67. List of custom fonts: https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts
  68.  
  69. Note about custom font:
  70. * Text background color is not supported for custom fonts. For these reason you would need to draw a filled
  71. rectangle before drawing the text. But this would cause the text to flicker, so I don't recommend using custom fonts
  72. for components that refresh continuously.
  73. * Using custom fonts slows down the arduino loop, so the refresh rate is lesser than using the standard font.
  74.  
  75.  
  76. Sketch made by: InterlinkKnight
  77. Last modification: 01/11/2018
  78. */
  79.  
  80.  
  81.  
  82. #include <Adafruit_GFX.h> // Include core graphics library
  83. #include <Adafruit_ST7735.h> // Include Adafruit_ST7735 library to drive the display
  84.  
  85.  
  86. // Declare pins for the display:
  87. #define TFT_CS 10
  88. #define TFT_RST 9 // You can also connect this to the Arduino reset in which case, set this #define pin to -1!
  89. #define TFT_DC 8
  90. // The rest of the pins are pre-selected as the default hardware SPI for Arduino Uno (SCK = 13 and SDA = 11)
  91.  
  92.  
  93. // Create display:
  94. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  95.  
  96.  
  97.  
  98. #include <Fonts/FreeSerif18pt7b.h> // Add a custom font
  99.  
  100.  
  101.  
  102.  
  103.  
  104. int Variable1; // Create a variable to have something dynamic to show on the display
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. void setup() // Start of setup
  112. {
  113.  
  114. // Display setup:
  115.  
  116. // Use this initializer if you're using a 1.8" TFT
  117. tft.initR(INITR_BLACKTAB); // Initialize a ST7735S chip, black tab
  118.  
  119. tft.fillScreen(ST7735_BLACK); // Fill screen with black
  120.  
  121. //tft.setRotation(0); // Set orientation of the display. Values are from 0 to 3. If not declared, orientation would be 0,
  122. // which is portrait mode.
  123.  
  124. tft.setTextWrap(false); // By default, long lines of text are set to automatically β€œwrap” back to the leftmost column.
  125. // To override this behavior (so text will run off the right side of the display - useful for
  126. // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
  127. // with setTextWrap(true).
  128.  
  129.  
  130.  
  131.  
  132. // We are going to print on the display everything that is static on the setup, to leave the loop free for dynamic elements:
  133.  
  134. // Write to the display the text "Hello":
  135. tft.setCursor(0, 0); // Set position (x,y)
  136. tft.setTextColor(ST7735_WHITE); // Set color of text. First is the color of text and after is color of background
  137. tft.setTextSize(3); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  138. tft.println("Hello"); // Print a text or value
  139.  
  140.  
  141.  
  142. // Start using a custom font:
  143. tft.setFont(&FreeSerif18pt7b); // Set a custom font
  144. tft.setTextSize(0); // Set text size. We are using custom font so you should always set text size as 0
  145.  
  146. // Write to the display the text "World":
  147. tft.setCursor(0, 50); // Set position (x,y)
  148. tft.setTextColor(ST7735_RED); // Set color of text. We are using custom font so there is no background color supported
  149. tft.println("World!"); // Print a text or value
  150.  
  151. // Stop using a custom font:
  152. tft.setFont(); // Reset to standard font, to stop using any custom font previously set
  153.  
  154.  
  155.  
  156.  
  157. // Draw rectangle:
  158. tft.drawRect(0, 60, 60, 30, ST7735_CYAN); // Draw rectangle (x,y,width,height,color)
  159. // It draws from the location to down-right
  160.  
  161. // Draw rounded rectangle:
  162. tft.drawRoundRect(68, 60, 60, 30, 10, ST7735_CYAN); // Draw rounded rectangle (x,y,width,height,radius,color)
  163. // It draws from the location to down-right
  164.  
  165.  
  166. // Draw triangle:
  167. tft.drawTriangle(60,120, 70,94, 80,120, ST7735_YELLOW); // Draw triangle (x0,y0,x1,y1,x2,y2,color)
  168.  
  169.  
  170. // Draw filled triangle:
  171. tft.fillTriangle(100,120, 110,94, 120,120, ST7735_CYAN); // Draw filled triangle (x0,y0,x1,y1,x2,y2,color)
  172.  
  173.  
  174. // Draw line:
  175. tft.drawLine(0, 125, 127, 125, ST7735_CYAN); // Draw line (x0,y0,x1,y1,color)
  176.  
  177.  
  178. // Draw circle:
  179. tft.drawCircle(15, 144, 14, ST7735_GREEN); // Draw circle (x,y,radius,color)
  180.  
  181.  
  182. // Draw a filled circle:
  183. tft.fillCircle(60, 144, 14, ST7735_BLUE); // Draw circle (x,y,radius,color)
  184.  
  185.  
  186. // Draw rounded rectangle and fill:
  187. tft.fillRoundRect(88, 130, 40, 27, 5, 0xF81B); // Draw rounded filled rectangle (x,y,width,height,color)
  188.  
  189. } // End of setup
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. void loop() // Start of loop
  198. {
  199.  
  200. Variable1++; // Increase variable by 1
  201. if(Variable1 > 150) // If Variable1 is greater than 150
  202. {
  203. Variable1 = 0; // Set Variable1 to 0
  204. }
  205.  
  206.  
  207. // Convert Variable1 into a string, so we can change the text alignment to the right:
  208. // It can be also used to add or remove decimal numbers.
  209. char string[10]; // Create a character array of 10 characters
  210. // Convert float to a string:
  211. dtostrf(Variable1, 3, 0, string); // (<variable>,<amount of digits we are going to use>,<amount of decimal digits>,<string name>)
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. // We are going to print on the display everything that is dynamic on the loop, to refresh continuously:
  220.  
  221. // Write to the display the Variable1 with left text alignment:
  222. tft.setCursor(13, 67); // Set position (x,y)
  223. tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // Set color of text. First is the color of text and after is color of background
  224. tft.setTextSize(2); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  225. tft.println(Variable1); // Print a text or value
  226.  
  227. // There is a problem when we go, for example, from 100 to 99 because it doesn't automatically write a background on
  228. // the last digit we are not longer refreshing. We need to check how many digits are and fill the space remaining.
  229. if(Variable1 < 10) // If Variable1 is less than 10...
  230. {
  231. // Fill the other digit with background color:
  232. tft.fillRect(23, 67, 12, 18, ST7735_BLACK); // Draw filled rectangle (x,y,width,height,color)
  233. }
  234. if(Variable1 < 100) // If Variable1 is less than 100...
  235. {
  236. // Fill the other digit with background color:
  237. tft.fillRect(36, 67, 12, 18, ST7735_BLACK); // Draw filled rectangle (x,y,width,height,color)
  238. }
  239.  
  240.  
  241.  
  242.  
  243. // Write to the display the string with right text alignment:
  244. tft.setCursor(81, 67); // Set position (x,y)
  245. tft.setTextColor(ST7735_GREEN, ST7735_BLACK); // Set color of text. First is the color of text and after is color of background
  246. tft.setTextSize(2); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  247. tft.println(string); // Print a text or value
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256. // We are going to write the Variable1 with a custom text, so you can see the issues:
  257.  
  258. // Draw a black square in the background to "clear" the previous text:
  259. // This is because we are going to use a custom font, and that doesn't support brackground color.
  260. // We are basically printing our own background. This will cause flickering, though.
  261. tft.fillRect(0, 90, 55, 34, ST7735_BLACK); // Draw filled rectangle (x,y,width,height,color)
  262.  
  263.  
  264.  
  265.  
  266. // Start using a custom font:
  267. tft.setFont(&FreeSerif18pt7b); // Set a custom font
  268. tft.setTextSize(0); // Set text size. We are using custom font so you should always set text size as 0
  269.  
  270. // Write to the display the Variable1:
  271. tft.setCursor(0, 120); // Set position (x,y)
  272. tft.setTextColor(ST7735_MAGENTA); // Set color of text. We are using custom font so there is no background color supported
  273. tft.println(Variable1); // Print a text or value
  274.  
  275. // Stop using a custom font:
  276. tft.setFont(); // Reset to standard font, to stop using any custom font previously set
  277.  
  278.  
  279.  
  280.  
  281.  
  282. } // End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement