Вы находитесь на странице: 1из 3

Smart product design Assignment 1-1: Using the OLED screen

In this assignment, we will learn two things:


Question

Try the following code, replace “your name” as your name


1. Using the OLED screen by I2C #include <Adafruit_GFX.h>
2. Using for loop for a simple algorithm #include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
Connect your OLED to screen via breadboard
void setup() {
//Here we init the display with I2C connection
//0x3C is the address (I2C address) of the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//delay 100ms
delay(100);
}

void loop() {
// Clear the buffer.
display.clearDisplay();
//Set text size
display.setTextSize(1);
//Set the color
display.setTextColor(WHITE);
//put cursor in the begining, otherwise text will scroll
display.setCursor(0,0);
Take care: 5V-5V, GND-GND, SCL-SCL, SDA-SDA
//Show my name
display.println("Your name");
What is SCL, SDA and I2C, learn from here
//show it on display
https://learn.sparkfun.com/tutorials/i2c display.display();

Learn how to install a library for Arduino delay(100);


}
https://www.arduino.cc/en/Guide/Libraries

Install Adafruit GFX, zip file is here


https://github.com/adafruit/Adafruit-GFX-Library

Install Adafruit SSD1306, zip file is here


https://github.com/adafruit/Adafruit_SSD1306

Page 38
Smart product design What is I2C?
I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, packet switched, single-ended, serial computer bus invented in 1982 by Philips
I2C Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board
communication. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).

There are just two wires, called SCL and SDA. SCL is the clock line. It is used to synchronize all data transfers over the I2C bus. SDA is the data line. The SCL & SDA
The lines are connected to all devices on the I2C bus. There needs to be a third wire which is just the ground or 0 volts. There may also be a 5volt wire is power is being
physical distributed to the devices.
I2C bus
(So we use 4 wires: +5v, GND, SCL and SDA)

Examples:
Each device has a unique address

The software limiting factor is the size of the address used for the slaves: 7-bit, 8-bit, or 10-bit, which support 127, 255, and 1023 devices, respectively. Physically, there are
Number of
two restrictions. First, the physical size of the bus because the bus is only meant for short runs. If the bus is too large there are capacitive loading and propagation delay
devices
effects that need to be dealt with. Second, some devices can't support the full range of I2C addresses.

There is a built in I2C in modern microcontrollers (including the Arduino). The transfer speed
is about 100K bits/s – but some is up to 400K bits/s.

Details of I2C is rather complicated. In the left figure, we try to explain the basics. However,
we suggest goto I2C serial communication:

https://www.youtube.com/watch?v=7CgNF78pYQM

For a better explanation.

Page 39
Smart product design Assignment 1-2: Experience an algorithm
Now we want to sum all integer numbers between 0 and n Now:
A. Draw a flowchart of a program which is able to:
Display
in 1. your name;
Mathematically, it is: sum  i
i0
2. the final 4 digits of your study number
(if it starts with a 0, then please use number 4321)
3. and the average of all integer numbers from 0 to
the above number on the OLED screen.
Study for loop:
https://www.arduino.cc/reference/en/language/structure/control-
B: implement this program
structure/for/
Try the following code, replace “your name” as your name,
You should upload an image to the bright space similar to the following image.
replace 9999 as the final four digits of your study number
void setup() {
Serial.begin(9600); Success
}

void loop() {

float sum = 0;

int i;

int n = 9999;

for (i=0;i<=n;i++) Your


{ flowchart
sum = sum + i; here
} (hand drawing
is OK) Your name
4 digits
Serial.println(sum); The average

delay(100);
}

Change float to int, try to understand data types Attention:


int - use 2 byte – range: -32,768 to 32,767 JPG file only to Brightspace, PDF and
The range is not enough to present the sum of each integer Word are not accepted
between 0 and 9999
Details:
https://playground.arduino.cc/Code/DatatypePractices Page 40

Вам также может понравиться