This is my I2C lab. I chose to play with the OLED screen to get synchronous communication. First I attached the screen and built the circuit with a potentiometer sensor. Connected the SCL and SDA pins to the top right pins on the Arduino Uno and the potentiometer sensor to pin A0 as well.
I hooked up the following code to get readings from the potentiometer and display them in the OLED screen.
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Wire.h>
#include <Fonts/FreeSans9pt7b.h>
const int SCREEN_WIDTH = 128; // OLED display width in pixels
const int SCREEN_HEIGHT = 64; // OLED display height in pixels
//initialize the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if(!Serial) delay(3000);
//first parameter of begin() sets voltage source
//SSD1306_SWITCHCAPVCC is for 3.3V
//second parameter is the I2C adress which is
//0x3C or 3D for some 128x64 modules
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
Serial.println("Display setup failed");
while(true);
}
Serial.println("Display is good to go");
display.setFont(&FreeSans9pt7b);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorReading = analogRead(A0);
//clear the display
display.clearDisplay();
//set the text size to 2
display.setTextSize(2);
//set the text color to white
display.setTextColor(SSD1306_WHITE);
// move the cursor to 0,0
display.setCursor(0,0);
//print the seconds
display.print("secs:");
display.print(millis() / 1000);
//move the cursor down 20pixels
display.setCursor(0,20);
//print a sensor reading
display.print("sensor:");
display.print(sensorReading);
//push everything out to the screen
display.display();
}
After that, I uploaded the following code that painted a line graph reading of the value of the potentiometer.
//by Ivan Abogado
//based on the Adafruit_SSD1306 library examples and Tom Igoe's version
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Wire.h>
#include <Fonts/FreeSans9pt7b.h>
const int SCREEN_WIDTH = 128; // OLED display width in pixels
const int SCREEN_HEIGHT = 64; // OLED display height in pixels
//initialize the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);
int xPos = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if(!Serial) delay(3000);
//first parameter of begin() sets voltage source
//SSD1306_SWITCHCAPVCC is for 3.3V
//second parameter is the I2C adress which is
//0x3C or 3D for some 128x64 modules
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
Serial.println("Display setup failed");
while(true);
}
Serial.println("Display is good to go");
// display.setFont(&FreeSans9pt7b);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorReading = analogRead(A0);
int graphHeight = SCREEN_HEIGHT - map(sensorReading, 0, 1023, 0, SCREEN_HEIGHT);
display.drawLine(xPos, SCREEN_HEIGHT, xPos, graphHeight, SSD1306_WHITE);
xPos++;
if (xPos > SCREEN_WIDTH){
xPos = 0;
//clear the display
display.clearDisplay();
}
// int16_t sensorReading = analogRead(A0);
// int16_t rect = map(sensorReading, 0, 1023, 0, SCREEN_HEIGHT);
// testdrawrect(rect);
//push everything out to the screen
display.display();
}
// void testdrawrect(int16_t step) {
// display.clearDisplay();
// for(int16_t i=0; i<display.height()/2; i+=step) {
// display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE);
// display.display(); // Update screen with each newly-drawn rectangle
// delay(1);
// }
delay(2000);
}
Finally I tried to play with the potentiometer to control the number of rectangles drawn in the screen canvas.
I used the following code based on the library examples and Tom Igoe’s version.
//by Ivan Abogado
//based on the Adafruit_SSD1306 library examples and Tom Igoe's version
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Wire.h>
#include <Fonts/FreeSans9pt7b.h>
const int SCREEN_WIDTH = 128; // OLED display width in pixels
const int SCREEN_HEIGHT = 64; // OLED display height in pixels
//initialize the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);
int xPos = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if(!Serial) delay(3000);
//first parameter of begin() sets voltage source
//SSD1306_SWITCHCAPVCC is for 3.3V
//second parameter is the I2C adress which is
//0x3C or 3D for some 128x64 modules
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
Serial.println("Display setup failed");
while(true);
}
Serial.println("Display is good to go");
// display.setFont(&FreeSans9pt7b);
}
void loop() {
// put your main code here, to run repeatedly:
// int sensorReading = analogRead(A0);
// int graphHeight = SCREEN_HEIGHT - map(sensorReading, 0, 1023, 0, SCREEN_HEIGHT);
// display.drawLine(xPos, SCREEN_HEIGHT, xPos, graphHeight, SSD1306_WHITE);
// xPos++;
// if (xPos > SCREEN_WIDTH){
// xPos = 0;
// //clear the display
// display.clearDisplay();
// }
int16_t sensorReading = analogRead(A0);
int16_t rect = map(sensorReading, 0, 1023, 0, SCREEN_HEIGHT);
testdrawrect(rect);
//push everything out to the screen
display.display();
}
void testdrawrect(int16_t step) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=step) {
display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(2000);
}