You are on page 1of 232

Arduino

51099 36

04-8325434

04-8325434

tyc6095@ms1.hinet.net
102 8

600

ISBN

9789868936027

Windows/Mac

51099 36

PDF

04-8325434

2D
PDF Reader

tyc6095@ms1.hinet.net

Copyright All Reserved


~ ~

~1~

Arduino
Arduino

idea

Arduino
Arduino

Arduino
Arduino
~2~


follow

~3~

2013


(ICT)

ICT

Arduino
Arduino
Arduino
Arduino

ICT

DIY(Do It Yourself)

~4~

Arduino

2013

~5~


........................................................................................................................2
........................................................................................................................4
......................................................................................................................6
....................................................................................................................9
..................................................................................................................11
......................................................................................................13
Arduino .................................................................................................16
Arduino ..............................................................................................16
Arduino ...............................................................................................18
Arduino ...............................................................................19
......................................................................................................22
..............................................................................................................26
..............................................................................................................27
......................................................................................................31
......................................................................................................33
......................................................................................................39
......................................................................................43
......................................................................................................47
......................................................................................................49
......................................................................................................54
......................................................................................................55
~6~

......................................................................................................60
..............................................................................................................62
......................................................................................62
......................................................................................................65
..........................................................................................................67
Strain gauge .........................................................70
Strain gauge .............................................................................72
..............................................................................................73
......................................................................................................76
Analog to Digital Coverters (ADC) .................................................78
Analog to Digital Converters ......................................................78
/A/D..............................................84
......................................................................................................97
..............................................................................................................99
..................................................................................99
............................................................................106
........................................................113
....................................................................................................116
............................................................................................................118
....................................................................118
....................................................................120
LCD ...................................................................................................122
LCD KeyPad Shield ....................................................................................130
~7~

LCD ......................................................................135
....................................................................................................140
............................................................................................................142
........................................................................................142
............................................................................................143
....................................................................................................144
........................................................................148
................................................................................154
Arduino .....................................................................................159
........................................................162
....................................................................................................168
............................................................................................................170
....................................................................................................................171
HX711 Arduino .............................................................................171
LCD 1602 .....................................................................................175
LCD&Keypad Shield ...................................................................189
DS1307 .........................................................................................195
HX711 ......................................................................................202
LCDKeypad Shield ...............................................................211
LCM 1602 ................................................................................212
............................................................................................................228

~8~


1 Arduino Duemilanove ...............................................20
2 Arduino UNO ............................................................21
3 Arduino Mega2560 ...................................................22
4 ............................................................................................62
5 .........................................................................69
6 ............................................................................69
7 ........................................................................70
8 ........................................................................................73
9 ........................................................................................74
10 .......................................................75
11 HX711 ADC .........................................................................78
12 HX711 ADC .................................................................79
13 AD ......................................................82
14 A 128db .......................................................................83
15 HX711 ........................................84
16 HX711 ..........................................................................86
17 HX711 SOP-16 ....................................................................86
18 HX711 ..................................................................................94
19 HX711 ......................................................................95
20 HX711 ..................................................................96
21 ....................................................................101
22 ()..................................................102
23 100 .............................................................102
~9~

24 ( 100 )..........................................103
25 500 .............................................................104
26 ( 500 )..........................................105
27 ....................................................................107
28 ()......................................108
29 100 .................................109
30 ( 100 )......................109
31 500 .................................110
32 ( 500 )...................... 111
33 ADC .................................................112
34 ............................................................113
35 ............................................116
36 ........................................................118
37 LCD1602 ...............................................................................123
38 LCD 1602 ...................................................................125
39 LCD 1602 ...........................................................................125
40 LCD Keypad Shield........................................................................131
41 LCD Keypad Shield PCB Layout ...........................................132
42 keypadshield ..................................................................135
43 ........................................................140
44 USB TTL ..........................................................................153
45 ............................................................153
46 .....................................................159
47 Tiny RTC I2C ................................................................160
48 ............................168
~ 10 ~


1 ................................................................................49
2 HX711 SOP-16 ()....................................................87
3 HX711 SOP-16 ..............................................................88
4 HX711 () ................................................................89
5 HX711 ...........................................................................90
6 A/D ........................................................93
7 HX711 ....................................................................................94
8 HX711 ..............................................99
9 HX711 Arduino ...........................................100
10 ()..............................................................101
11 ( 100 ) ......................................................103
12 ( 500 )......................................................104
13 ()..............................................107
14 ( 100 )......................................109
15 ( 500 )..................................110
16 ............................................................................112
17 ....................................................................114
18 .....................................................115
19 ....................................119
20 HX711 Arduino ..................................................................120
21 ................................................................121
22 LCD1602 ...................................................................123
~ 11 ~

23 LCD Keypad Shield ...........................................................132


24 LCD Keypad Shield Arduino ............................136
25 HX711 Arduino .........................................136

~ 12 ~

~ 13 ~

Arduino C

~ 14 ~

Arduino

~ 15 ~

Arduino
Arduino
Massimo Banzi Ivrea
2005 Massimo Banzi
David Cuartielles David Cuartielles

Banzi David Mellis


David Mellis
Arduino
Arduino

Arduino

BanziCuartielles Mellis
(Open Source)
CC (Creative_Commons, 2013)
CC(Creative_Commons, 2013)
GPL1license
2 (Free Software Foundation)
GNU (GNU GPL) CC

1 GNU GNU General Public License GNU GPL


GPL GNU
2

~ 16 ~

Arduino

Arduino
CC Arduino

Arduino Arduino 3
Arduino
Arduino (Arduino, 2013)
ArduinoMassimo BanziDavid Cuartielles
Tom IgoeGianluca MartinoDavid Mellis Nicholas Zambetti(Arduino,
2013) Arduino Arduino
http://www.arduino.cc/
Arduino
Atmel

AVR

(Atmel_Corporation, 2013)
Simple I/O Java C
Processing 4 /Wiring (Reas, 2013, Reas and Fry, 2007, Reas and Fry,
2010)Processing MIT (Aesthetics & Computation
Group) Ben Fry(http://benfry.com/) Casey Reas Processing
Open Source

Arduino Arduino

Processing Open Source

~ 17 ~

SwitchLED
Arduino IDE
(Banzi, 2009)

Arduino

http://www.arduino.cc/!!

Arduino ISCP IC bootloader


(http://arduino.cc/en/Hacking/Bootloader?from=Main.Bootloader)

(http://www.arduino.cc/) Arduino

(,,,
,,)

(ATMEGA8-16)

USB 9VDC

ArduinoCCD

~ 18 ~

Arduino
Duemilanove

:USB
: ATMEGA328
USB/

ATmega328
5V
(recommended)7-12 V
(limits)6-20 V

I/O Pins14 (of which 6 provide PWM output)


Pins6
DC Current per I/O Pin40 mA
DC Current for 3.3V Pin50 mA
Flash Memory32 KB (of which 2 KB used by bootloader)
SRAM2 KB
EEPROM1 KB
Clock Speed16 MHz

bootloader5
Arduino
Arduino USB

boot loader

~ 19 ~

1 Arduino Duemilanove

UNO
ATMega 8U2 USB- ICSP
MEGA8U2 ~ FTDI
USB ~ Arduino IC 3.3V

ATmega328
5V
(recommended)7-12 V
(limits)6-20 V
I/O Pins14 (of which 6 provide PWM output)
Pins6
DC Current per I/O Pin40 mA
DC Current for 3.3V Pin50 mA
Flash Memory32 KB (of which 0.5 KB used by bootloader)
SRAM2 KB
EEPROM1 KB
Clock Speed16 MHz

~ 20 ~

2 Arduino UNO

MEGA2560
Arduino

ATmega2560
5V
(recommended)7-12 V
(limits)6-20 V
I/O Pins54 (of which 14 provide PWM output)
UART:4
Pins16
DC Current per I/O Pin40 mA
DC Current for 3.3V Pin50 mA
Flash Memory256 KB of which 8 KB used by bootloader
SRAM8 KB
EEPROM4 KB
Clock Speed16 MHz

~ 21 ~

3 Arduino Mega2560

Arduino
Language Reference (http://arduino.cc/en/Reference/HomePage)
Arduino

setup()
loop()

Arduino (SKETCH)
~ 22 ~


void setup()
Arduino -
(loop()) Arduino pin

void loop()
Arduino
Arduino

(block structure)
C {}
C
(Compound Statement)

(
) (CPU
)

~ 23 ~

(naming space)
C (naming space)

(local variable) ()
int
z

{
int x, r;
x=10;
r=20;
{
int y, z;
float r;
y = x;
x = 1;
r = 10.5;
}
z = x; // z
}


r
(scope)

(Lifetime)

~ 24 ~




r r float

C

{
int tmp;
for (tmp=0; tmp<1000; tmp++)
doSomeThing();
}
{
float tmp;
tmp = y;
y = x;
x = y;
}
tmp
tmp

; (semicolon)
{} (curly braces)
// (single line comment)
/* */ (multi-line comment)
Arduino
~ 25 ~

; //()
Arduino

delay(100);

{}()
loop()

void loop(){
Serial.pritln("Hello !! Welcome to Arduino world");
}

(
)
Arduino
Arduino
~ 26 ~

//
/*


*/

(identifier) C

int char C (keyword)


31

(declaration)

HIGH | LOW
INPUT | OUTPUT
true | false
Integer Constants

~ 27 ~

boolean
char
byte
int
unsigned int
long
unsigned long
float
double
string
array
void

Arduino HIGH LOW


Arduino (pin)INPUT OUTPUT
Arduino (pin)true false

Arduino

Arduino

Arduino :
boolean
~ 28 ~

(true)(false)
char
A Arduino

-128 127
PS ASCII UNICODE

ASCII 127

UNICODE

ASCII

byte
0 255
(8 )
int
2 32,768
32,767; Arduino
unsigned int
2
0 65,535
~ 29 ~

long

2,147,483,648 2,147,483,647
unsigned long
0 4,294,967,295
float

RAM
double
1.7976931348623157 x 10308
string
ASCII (
)
Ardunio

char word1 = "Arduino world"; // 7 + 1


char word2 = "Arduino is a good developed kit"; //

array
LED
light01light02light03light04light05light06

int light = {0, 20, 40, 65, 80, 100};


~ 30 ~

"array" []{}

char()
byte()
int()
long()
float()

char()

char(x)

x:

byte()

byte(x)

x:
~ 31 ~

int(x)

int(x)

x:

long()

int(x)

x:

float()

float(x)
~ 32 ~


x:

if
if...else
for
switch case
while
do... while
break
continue
return

Ardunio

if else
If (expression)
else .
if else

~ 33 ~


#define LED 12
void setup()
{
int val =1;
if (val == 1) {
digitalWrite(LED,HIGH);
}
}
void loop()
{
}

for

void setup()
{
for (int i = 1; i < 9; i++) {
Serial.print("2 * ");
Serial.print(i);
Serial.print(" = ");
Serial.print(2*i);
}
}
void loop()
{
}

switch case

~ 34 ~

if switch case swith


case if swith
case
:
void setup()
{
int sensorValue;
sensorValue = analogRead(1);
switch (sensorValue) {
case 10:
digitalWrite(13,HIGH);
break;
case 20:
digitalWrite(12,HIGH);
break;
default: //
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
}
void loop()
{
}

while
while
:
void setup()
{
~ 35 ~

int sensorValue;
// sensor 256 LED 1
sensorValue = analogRead(1);
while (sensorValue < 256) {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
}
}
void loop()
{
}

do while
while while

dowhile
:
void setup()
{
int sensorValue;
do
{
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
}
while (sensorValue < 256);
}
void loop()
~ 36 ~

{
}

break
Break
break switch case
:
void setup()
{
}
void loop()
{
int sensorValue;
do {
//
if (digitalRead(7) == HIGH)
break;
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
}
while (sensorValue < 512);
}

continue
continue

~ 37 ~

:
#define PWMpin 12
#define Sensorpin 8
void setup()
{
}
void loop()
{
int light;
int x ;
for (light = 0; light < 255; light++)
{
// 140 200
x = analogRead(Sensorpin) ;
if ((x > 140) && (x < 200))
continue;
analogWrite(PWMpin, light);
delay(10);
}
}

return
return
computeTemperature()
temperature
#define PWMpin 12
#define Sensorpin 8
void setup()
{
~ 38 ~

}
void loop()
{
int light;
int x ;
for (light = 0; light < 255; light++)
{
// 140 200
x = computeTemperature() ;
if ((x > 140) && (x < 200))
continue;
analogWrite(PWMpin, light);
delay(10);
}
}
int computeTemperature() {
int temperature = 0;
temperature = (analogRead(Sensorpin) + 45) / 100;
return temperature;
}

= ()

+ ()

- ()

* ()
~ 39 ~

/ ()

% ()

Arduino +
* /
(%)

()
:
#define PWMpin 12
#define Sensorpin 8
void setup()
{
int sensorValue;
int light;
int remainder;
sensorValue = analogRead(Sensorpin) ;
light = ((12 * sensorValue) - 5 ) / 2;
remainder = 3 % 2;
}
void loop()
{
}

== ()
~ 40 ~

!= ()

< ()

> ()

<= ()

>= ()

if,while, for

==

a==1

!=

a!=1

<

a<1

>

a>1

<=

a<=1

>=

a>=1

&& (and)
|| (or)
! (not)

5 10
#define PWMpin 12
#define Sensorpin 8
void setup()
{
~ 41 ~

}
void loop()
{
int light;
int sensor ;
for (light = 0; light < 255; light++)
{
// 140 200
sensor = analogRead(Sensorpin) ;
if ((sensor >= 5) && (sensor <=10))
continue;
analogWrite(PWMpin, light);
delay(10);
}
}

: (and) && ; (or) || ;


(finally not) !

1:
Int value = 10 ;
value = value + 1 ;

(++)
Int value = 10 ;
value ++;

~ 42 ~

++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)

(++ --)
1 1 i++++i
i++i i i+1++i i
i i 1
+= , =, *= and /=

Int value = 10 ;
value = value +5 ;
value += 5 ;

// ()
// ()

pinMode()
digitalWrite()
digitalRead()

analogRead()
~ 43 ~

analogWrite() - PWM

Arduino

pinMode(pin, mode)
(digital pin)

#define sensorPin 7
#define PWNPin 8
void setup()
{
pinMode(sensorPin,INPUT); // sensorPin (7)
}
void loop()
{
}
:

digitalWrite(pin, value)
pinMode
digitalWrite
:
#define PWNPin 8
#define sensorPin 7
void setup()
{
digitalWrite (PWNPin,OUTPUT); // PWNPin (8)
}
void loop()
~ 44 ~

{}

int digitalRead(pin)
HIGH
LOW
:
#define PWNPin 8
#define sensorPin 7
void setup()
{
pinMode(sensorPin,INPUT); // sensorPin (7)
val = digitalRead(7); // 7 val
}
void loop()
{
}

int analogRead(pin)
0 1023 0 5

:
#define PWNPin 8
#define sensorPin 7
void setup()
{
pinMode(sensorPin,INPUT); // sensorPin (7)
val = analogRead (7); // 7 val
}
void loop()
{
~ 45 ~

analogWrite(pin, value)
PWM 356910 11value
0-255 2.5 V 128
:
#define PWNPin 8
#define sensorPin 7
void setup()
{
analogWrite (PWNPin,OUTPUT); // PWNPin (8)
}
void loop()
{
}

I/O

tone()
noTone()
shiftOut()
pulseIn()

shiftOut(dataPin, clockPin, bitOrder, value)

bitOrder LSBFIRST
MSBFIRST value byte

:
~ 46 ~

#define dataPin 8
#define clockPin 7
void setup()
{
shiftOut(dataPin, clockPin, LSBFIRST, 255);
}
void loop()
{
}

unsigned long pulseIn(pin, value)

:
#define dataPin 8
#define pulsein 7
void setup()
{
Int time ;
time = pulsein(pulsein,HIGH); // 7
HIGH
}
void loop()
{
}

millis()
micros()
delay()
delayMicroseconds()
~ 47 ~

unsigned long millis()

:
int lastTime ,duration;
void setup()
{
lastTime = millis() ;
}
void loop()
{
duration = -lastTime; // "lastTime"
}

delay(ms)

:
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(millis()) ;
delay(500); //500
}

10 3 10 3
0.001 1
~ 48 ~

pico

10 12

nano

10 9

micro

10 6

milli

10 3

kilo

10 3

mega

10 6

giga

10 9

tera

tera

delay Microseconds(us)

void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(millis()) ;
delayMicroseconds (1000); //500

min()
max()
abs()
constrain()
~ 49 ~

map()
pow()
sq()
sqrt()

min(x, y)

#define sensorPin1 7
#define sensorPin2 8
void setup()
{
int val;
pinMode(sensorPin1,INPUT); // sensorPin1 (7)
pinMode(sensorPin2,INPUT); // sensorPin2 (8)
val = min(analogRead (sensorPin1), analogRead (sensorPin2)) ;
}
void loop()
{
}

max(x, y)

#define sensorPin1 7
#define sensorPin2 8
void setup()
{
int val;
pinMode(sensorPin1,INPUT); // sensorPin1 (7)
pinMode(sensorPin2,INPUT); // sensorPin2 (8)
val = max (analogRead (sensorPin1), analogRead (sensorPin2)) ;
~ 50 ~

}
void loop()
{
}

abs(x)

#define sensorPin1 7
void setup()
{
int val;
pinMode(sensorPin1,INPUT); // sensorPin (7)
val = abs(analogRead (sensorPin1)-500);
// -500
}
void loop()
{
}

constrain(x, a, b)
x a b x a a a b
x b b

#define sensorPin1 7
#define sensorPin2 8
#define sensorPin 12
void setup()
{
int val;
pinMode(sensorPin1,INPUT); // sensorPin1 (7)
pinMode(sensorPin2,INPUT); // sensorPin2 (8)
~ 51 ~

pinMode(sensorPin,INPUT); // sensorPin (12)


val = constrain(analogRead(sensorPin), analogRead (sensorPin1),
analogRead (sensorPin2)) ;
// 255
}
void loop()
{
}

map(value, fromLow, fromHigh, toLow, toHigh)


value fromLow fromHigh toLow toHigh

#define sensorPin1 7
#define sensorPin2 8
#define sensorPin 12
void setup()
{
int val;
pinMode(sensorPin1,INPUT); // sensorPin1 (7)
pinMode(sensorPin2,INPUT); // sensorPin2 (8)
pinMode(sensorPin,INPUT); // sensorPin (12)
val = map(analogRead(sensorPin), analogRead (sensorPin1), analogRead
(sensorPin2),0,100) ;
// analog0 100 200
}
void loop()
{
}

double pow(base, exponent)


(base)(exponent)

~ 52 ~


int y=2;
double x = pow(y, 32); // x y 32

double sqrt(x)
double

int y=2123;
double x = sqrt (y); // 2123

sin()
cos()
tan()

double sin(rad)
radians sine

int y=45;
double sine = sin (y);

// 0.70710678118654

double cos(rad)
radians cosine

int y=45;
~ 53 ~

double cosine = cos (y); // 0.70710678118654

double tan(rad)
radians tangent

int y=45;
double tangent = tan (y);

// 1

randomSeed()
random()

randomSeed(seed)
Arduino

#define sensorPin 7
void setup()
{
randomSeed(analogRead(sensorPin)); //
}
void loop()
{
~ 54 ~

long random(max)
long random(min, max)
long 0

#define sensorPin 7
long randNumber;
void setup(){
Serial.begin(9600);
// if analog input pin sensorPin(7) is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(sensorPin));
}
void loop() {
// print a random number from 0 to 299
randNumber = random(300);
Serial.println(randNumber);
// print a random number from 0 to 100
randNumber = random(0, 100); // 0 99
Serial.println(randNumber);
delay(50);
}

~ 55 ~

Serial.begin(speed)
Arduino 9600 bps
115,200 bps

void setup() {
Serial.begin(9600);
}
void loop() {
}

// open the serial port at 9600 bps:

Serial.print(data)

int x = 0;

// variable

void setup() {
Serial.begin(9600);
}

// open the serial port at 9600 bps:

void loop() {
// print labels
Serial.print("NO FORMAT");
Serial.print("\t");
Serial.print("DEC");
Serial.print("\t");
Serial.print("HEX");

// prints a label
// prints a tab

Serial.print("\t");
Serial.print("OCT");
Serial.print("\t");
Serial.print("BIN");
~ 56 ~

Serial.print("\t");
}

Serial.println(data)
Serial.println(data, encoding)
Serial.print()
Enter

int x = 0;
// variable
void setup() {
Serial.begin(9600);

// open the serial port at 9600 bps:

}
void loop() {
// print labels
Serial.print("NO FORMAT");
Serial.print("\t");
Serial.print("DEC");
Serial.print("\t");
Serial.print("HEX");
Serial.print("\t");
Serial.print("OCT");
Serial.print("\t");
Serial.print("BIN");
Serial.print("\t");

// prints a label
// prints a tab

for(x=0; x< 64; x++){


// only part of the ASCII chart, change to suit
// print it out in many formats:
Serial.print(x);
// print as an ASCII-encoded decimal - same as
"DEC"
Serial.print("\t");
// prints a tab
Serial.print(x, DEC); // print as an ASCII-encoded decimal
Serial.print("\t");
// prints a tab
Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal
Serial.print("\t");
// prints a tab
~ 57 ~

Serial.print(x, OCT); // print as an ASCII-encoded octal


Serial.print("\t");
// prints a tab
Serial.println(x, BIN); // print as an ASCII-encoded binary
//
then adds the carriage return with "println"
delay(200);
// delay 200 milliseconds
}
Serial.println("");

// prints another carriage return

int Serial.available()
bytes read()
0 read()

int incomingByte = 0; // for incoming serial data


void setup() {
Serial.begin(9600);
// opens serial port, sets data rate to 9600
bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

int Serial.read()
1byte

~ 58 ~


int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
// opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

int Serial.write()

void setup(){
Serial.begin(9600);
}
void loop(){
Serial.write(45); // send a byte with the value 45
int bytesSent = Serial.write("hello Arduino , I am a beginner in the
Arduino world");
}

Serial.flush()

~ 59 ~


(buffer)

void setup(){
Serial.begin(9600);
}
void loop(){
Serial.write(45); // send a byte with the value 45
int bytesSent = Serial.write("hello Arduino , I am a beginner in the
Arduino world");
Serial.flush();
}

Arduino

~ 60 ~

~ 61 ~

CPU
CPU

~ 62 ~

1
2
3
4

30Kg

30-300Kg

300Kg

I 10-6

II 10-5<<10-6

III 10-3<<10-5

IV 10-2<<10-3

1.
~ 63 ~

2.
3. ()
4.
5.
6.
7. ()
8.
9.
10. e
11.

1.

()

2.

3.

120%

4.

5.

6.

;d

7.

60Kg5g5g

8.

=( e )300g
0.001g0.001g

9.

10g 10g
100kg10g(1001000)10=1000010000

~ 64 ~

10.

6000g
0.5g 05/6000=1/12000 1/12000

11.

12.

13.

14.

CPU
Counting Scale

15.

16.

OFFSET COUNT

~ 65 ~

~ 66 ~


1971 Ralph S.Shoberg
(Shoberg, 1971, Shoberg, 1976, Shoberg, 1978)

4
(Load Cell)
5

~ 67 ~

6 (Strain Gage)(Window and Holister,


1982) 6
(Strain Gage)
7

(Hannah and Reed, 1992)

~ 68 ~

~ 69 ~

Strain gauge
()
resistivity

R=

L
Ac

.....(1)

resistivity
-m

Eq. (1)

dR =

Ac ( Ld + dL) LdAc
.....(2)
Ac

Eq. (2) Eq. (1)

dR d dL dAc
=
+

.....(3)
R

L
Ac

~ 70 ~


Poissons ratio
dAc
dL
= 2 .....(4)
Ac
L

Eq. (4) Eq. (3)


dR d dL
dL
=
+
2 .....(5)

R
L
L
Eq. (5) Eq. (6)

dR d
dL
=
+ (1 + 2 )
..... (6)
R

L
Gage Factor (GF)

d
dR
GF = R = 1 + 2 + .....(7)
dL
dL
L
L

d
Eq. (7) 0 Poisons ratio
0.3 GF = 1 + 2 1.6

dL
L (axial strain)

dL
.....(8)
L
~ 71 ~

Eq. (7) Eq. (8)= Gage Factor (GF)

dR
= GF .....(9)
R
(strain

gage)(Beckwith, et al., 2007)

Strain gauge
strain gauge, strain gage(
8 )1938 Edward E. Simmons(Simmons, 1942, Simmons,

1944) Arthur C. Ruge(C, 1943, Ruge, 1943)

(AA 502 )(Coover, 1956)


( 8 )

~ 72 ~

( 8 )

:(a).(Wire)(b).(foil)
()
8

Wheatstone bridge
1833 Samuel Hunter Christie 1843
(Stein, 1990)
~ 73 ~

9
(, et al., 2003)

R1 R2
=
R2 = R
R4 R3

( R 1 = R 3 = R 4 = R ) R2 = R + R

1 (, et al., 2003)

~ 74 ~

R + R
R
E0 =
Vexc
Vexc
R + R + R
R+R
R
R + R
=

Vexc
2 R + R 2 R
2 R 2 + 2 R R 2 R 2 + R R
=
Vexc
(2 R + R )(2 R )

R R

= 2
Vexc
4 R + 2 R R
R R
V
=
2 exc
4R

if 4 R 2 >> 2 R R

1 R
=
Vexc
4 R
1
(, et al., 2003)

strain gauge, strain gage 10

10
(, et al., 2003)

R1 = R3 = R R C
R2 = R4 = R + R T
~ 75 ~

(, et al., 2003)

R + R
R R

E0 =
Vexc
Vexc
R + R + R R
R + R + R R
R R + R + R
=
Vexc
2R

R
=
Vexc
R
R
Vexc
E0 =
R

2
(, et al., 2003)

(Load Cell)(Load Cell)

Arduino
( 10 )
24 Analog to Digital Converters (ADC)

~ 76 ~

ADC

~ 77 ~

Analog to Digital Coverters


(ADC)
Analog to Digital Converters
(, 2013)
HX711 ad(, 2013)
11 12
(MCU)

11 HX711 ADC

~ 78 ~

12 HX711 ADC
HX711 24 A/D

A
B A 128
6420mV 40mV B
64
A/D

HX711
4.8-5.5v
1.6mA
2.9cm * 1.7cm * 0.4cm
~ 79 ~

(STC15F104)


3264 128
A/D


Tx/Rx

10Hz 80Hz
50Hz 60Hz

< 1.7mA, < 1A
2.6 ~ 5.5V
-20 ~ +85

(Load Cell)
A A+ A~ 80 ~

B B+ B-

HX711DIOSCK
22k
J1
HX71180hz/10hz
4.7V5.5V5.5V
4.7V
BB+ B-
HX711
13 HX711
13

~ 81 ~

13 AD

Arduino 14
115200

/do/ck
1 A 128db

~ 82 ~

14 A 128db

HX711
15 HX7118STCSTC15F104
SOP8 15

~ 83 ~

15 HX711

/A/D
HX711 24A/D

MCU

A B
A 128 64
20mV 40mV B 32
~ 84 ~


A/D


64 128
A/D


Tx/Rx

10Hz 80Hz
50Hz 60Hz
< 1.7mA,
< 1A
2.6 ~ 5.5V
~ 85 ~

-20 ~ +85
16 HX711

16 HX711
(, 2013)
17 HX711Pin

SOP-16

17 HX711 SOP-16
(, 2013)

2 HX711
~ 86 ~

3 HX711

2 HX711 SOP-16 ()
Pin #

Name

Function

Description

VSUP

Power

Regulator supply: 2.7 ~ 5.5V

BASE

Analog Output

Regulator control outputNC when not used

AVDD

Power

Analog supply: 2.6 ~ 5.5V

VFB

Analog Input

Regulator control inputconnect to AGND


when not used

Ground

Analog Ground

VBG

Analog Output

Reference bypass output

INA-

Analog Input

Channel A negative input

INA+

Analog Input

Channel A positive input

INB-

Analog Input

Channel B negative input

10

INB+

Analog Input

Channel B positive input

11

PD_SCK

Power down control (high active) and serial

AGND

clock input
Digital Output

Serial data output

XO

Digital I/O

Crystal I/O (NC when not used

XI

Digital Input

Crystal I/O or external clock input, 0: use

12

DOUT

13
14

on-chip oscillator
15

RATE

Digital Input

Output data rate control, 0: 10Hz; 1: 80Hz

16

DVDD

Power

Digital supply: 2.6 ~ 5.5V

(, 2013)

~ 87 ~

3 HX711 SOP-16

VSUP

: 2.6 ~ 5.5V
AVDD

BASE

AVDD

: 2.6 ~ 5.5V

VFB

AGND

VBG

INA-

INA+

INB-

10

INB+

11

PD_SCK

Tx/Rx

12

DOUT

Tx/Rx

13

XO

14

XI

0:

15

RATE

0: 10Hz; 1: 80Hz

16

DVDD

: 2.6 ~ 5.5V

(, 2013)

4 HX711 Pin 5 HX711 Pin

~ 88 ~

4 HX711 ()
Parameter
Full scale

Notes
V(inp)-V(inn)

MIN

TYP

MAX

UNIT
V

0.5(AVDD/GAIN)

differential
input range
Common

mode

AGND+1.2

AVDD-1.3

input
Output data rate

Internal Oscillator, RATE = 0

10

Internal Oscillator, RATE =

80

DVDD

Hz

Crystal or external clock,

fclk/1,105,920

RATE = 0
Crystal or external clock,

fclk/138,240

RATE = DVDD
Output data coding

2s complement

Output settling time

RATE = 0

400

RATE = DVDD

50

Gain = 128

0.2

Gain = 64

0.4

Gain = 128RATE = 0

50

Gain = 128RATE = DVDD

90

Input offsetGain = 128

nV/

GainGain = 128

ppm/

Gain = 128RATE = 0

100

dB

Input offset drift

Input noise

Temperature drift

Input common

800000

mode
rejection

~ 89 ~

7FFFFF

HEX
50

mV

nV(rms)

Power

supply

Gain = 128RATE = 0

100

dB

1.25

rejection
Reference bypass
VBG
Crystal or external

11.0592

20

MHz

clock frequency
Power

supply

voltage
Analog

supply

current

(including

regulator)
Digital

supply

current

DVDD

2.6

5.5

AVDDVSUP

2.6

5.5

Normal

1400

Power down

0.3

Normal

100

Power down

0.2

(, 2013)

5 HX711

V(inp)-V(inn)

AGND+0.6 AVDD-0.6

0.5(AVDD/GAIN)

RATE = 0

V
V

10
RATE

Hz
80

= DVDD
RATE
= 0

~ 90 ~

fclk/1,105,920

RATE = DVDD

fclk/138,240

(1)

RATE = 0

800000 7FFFFF(HEX)
400

RATE = DVDD

= 128

50
0.2

= 64

ms

mV
0.8

= 128RATE = 0

50

= 128RATE =

nV(rms)
90

DVDD
7

nV/

= 128

ppm/

= 128RATE = 0

100

dB

= 128RATE = 0

100

dB

VBG

1.25

=
128

V
11.0592 30

MHz

DVDD

2.6 5.5

AVDDVSUP

2.6 5.5

1600

0.3

100

0.2

(, 2013)

~ 91 ~

A
A/D
128 64
20mV 40mV B 32
80mV B

(DVDD) MCU HX711


A/D
(VSUP) (DVDD)
VAVDD R1R2 VBG
1VAVDD=VBG(R1+R2)/R
(VSUP) 100mV VSUP
AVDD 2.6 5.5V
VBG VFB BASE

XI HX711

10Hz 80Hz
20pF XIXI XO

11.0592MHz,
10Hz 80Hz
MCU
~ 92 ~

20pF XI
150mV

Tx/Rx

Tx/RxPD_SCK DOUT
DOUT A/D
Tx/RxPD_SCK DOUT
PD_SCK 25 27
24
MSB24 24
2527 A/D
6

6 A/D

PD_SCK

25

128

26

32

27

64

PD_SCK 25 27Tx/Rx
A/D A/D 4
DOUT 4

~ 93 ~

18 HX711
7 HX711
Symbol

Note

MIN TYP MAX Unit

T1

DOUT falling edge to PD_SCK rising edge

0.1

T2

PD_SCK rising edge to DOUT data ready

T3

PD_SCK high time

0.2

T4

PD_SCK low time

0.2

s
0.1

50

s
s

(, 2013)

PD_SCK HX711 PD_SCK

~ 94 ~

19 HX711
(, 2013)

PD_SCK 60sHX711
19
A/D PD_SCK

A 128 A/D
PD_SCK Tx/Rx
A/D 4
DOUT 4

20 HX711
(XI=0)10Hz (RATE=0) 2.7
5.5V) MCU
PNP S8550 R1R2 A/D
A B
~ 95 ~

20

20 HX711
(, 2013)
\

HX711
1. RATE XI PD_SCK

2.
3. A B

4. PNP S8550
MOS
5. A/D

6. 25 27 Tx/Rx

~ 96 ~


HX711 Analog to Digital Converters (ADC)

Arduino

~ 97 ~

~ 98 ~


(Load Cell)
Arduino

13 (Load Cell)
HX711 8 (Load Cell)
HX711

HX711 ( 11 & 12)


Arduino 9 HX711 Arduino
9

8 HX711

HX711

E+()

+(:)

E-()

GND(:)

A+(/)

A+(:)

()

A-()

A-(:)

()

~ 99 ~

9 HX711 Arduino
HX711

Arduino

Do/Rx

Arduino digital output pin 8

DO/RX

Clock

Arduino digital output pin 9

CK/TX

Vcc

Arduino pin 5V

5V

GND

Arduino pin Gnd

Arduino sketch
Arduino

HX711 (hx711_1)
#include <HX711.h> // HX711 Arduino library
#define datapin 8
//
#define clockpin 9 //
HX711 hx(clockpin, datapin,128);
// HX711

void setup() {
Serial.begin(9600);
}
void loop()
{
double sum = 0;
//
10
for (int i = 0; i < 10; i++) //
sum += hx.read(); //

Serial.println(sum/10); //
}
21
10

~ 100 ~

21
10 ()
-14175.70
-14365.20
-14380.20
-14514.20
-14388.90
-14452.90
-14481.20
-14498.90
-14593.50
-14456.00
-14555.50
-14387.70
-14504.30

~ 101 ~

()
10000
0
-10000 1
-20000

26 51 76 101 126 151 176 201

0()

22 ()

23 100
11

23 100

~ 102 ~

11 ( 100 )
16838.60
16870.40
16894.50
16787.20
16744.50
16901.50
16845.60
16821.20
16844.90
16878.10
16890.00
16803.00
16875.00

(100)

20000
15000

10000

100()

5000
0
1

16 31 46 61 76 91 106 121 136 151 166 181 196

24 ( 100 )

25 500
12

~ 103 ~

25 500

12 ( 500 )
142037.90
142217.09
142129.40
142273.40
142258.40
142298.59
142302.20
142069.79
142172.90
141995.50
142230.20
142085.79
142271.50

~ 104 ~

(500)
200000
100000
0

28 55 82 109 136 163 190

500()

26 ( 500 )

10 -14370.82875
12 100 16876.45 500
142028.1371 11

a = -14370.82875

100 c=16876.45

500 b=142028.1371

p=

( ADC - ADC)
3

p=

( ADC - ADC)
~ 105 ~

p=

100

100
= 0.0032002787
( 16876.45 - (-14370.82875))

400 p =

500 

400
= 0.0031961215
(142028.1371 - 16876.45)

0.0031977841 0.0031977841

0.0031977841

27
13

Step2
HX711 (hx711_2)
#include <HX711.h> // HX711

Arduino library

#define datapin 8
//
#define clockpin 9 //
HX711 hx(clockpin, datapin,128, 0.0031977841 );

void setup() {
Serial.begin(9600);
}
void loop()
{
~ 106 ~

// HX711

HX711 (hx711_2)
double sum = 0;
//
10
for (int i = 0; i < 10; i++) //
sum += hx.read(); //

Serial.println(sum/10); //
}

27

13 ()
-13296.00
-13239.10
-13107.50
-12982.90
-13061.40
-13083.80
-13271.50
-13161.10
-13157.80
-13309.20
~ 107 ~

-13274.20
-13311.60
-13314.70

()

5000
0
-5000

17 33 49 65 81 97 113 129 145 161 177 193

0()

-10000
-15000

28 ()

29 100
11

~ 108 ~

29 100
14 ( 100 )
18026.40
18005.50
18081.40
17976.60
17976.80
18060.50
17986.70
17967.70
18020.10
18115.80
18246.00
18169.20
18078.40

(100)
20000
15000

10000

100

5000
0
1

15 29 43 57 71 85 99 113 127 141 155 169 183 197

30 ( 100 )

31 500
500 15

~ 109 ~

31 500
15 ( 500 )
143485.59
143496.20
143591.29
143614.90
143578.29
143534.70
143440.20
143461.59
143406.90
143367.59
143293.20
143509.20
143402.20

~ 110 ~

(500)
200000
150000

100000

500

50000
0
1

16 31 46 61 76 91 106 121 136 151 166 181 196

32 ( 500 )

13 -13117.91613
14 100 18148.34241 15
500 143325.8016

a =-13117.91613

100 b=18148.34241

500 c=143325.8016

p=

( ADC - ADC)

100
p=

100
= 0.0031983360
( 18148.34241 - (-13117.91613))

400
p=

400
= 0.0031954635
( 143325.8016 - 18148.34241)
~ 111 ~

500 

16
33 34
0.0031977841
0.0031977841
16

ADC ADC

100

31250.76067

31266.25854

0.003199922110

0.003198335991

0.000001586118

400

125148.2052

125177.4592

0.003196210440

0.003195463484

0.000000746956

500

156398.9659

156443.7178

0.003196952085

0.003196037573

0.000000914512

104265.9772

104295.8118

0.003197694878

0.003196612349

0.000001082529

ADC

ADC
40
ADC

20
0
-20

100

400

500

-40

33 ADC

~ 112 ~

ADC

0.003202000000
0.003200000000
0.003198000000
0.003196000000
0.003194000000
0.003192000000

100

400

500

34

0.0031977841
ADC -13117.91613

ADC -13117.91613 setup


bias_read()
read()

HX711 (hx711_3)
#include <HX711.h> // HX711

Arduino library

#define datapin 8
//
#define clockpin 9 //
HX711 hx(clockpin, datapin,128,0.0031977841);

void setup() {
hx.set_offset(-13117.91613);
Serial.begin(9600);
}
void loop() {
delay(500);
~ 113 ~

// HX711

HX711 (hx711_3)
double sum0 = 0;
double sum1 = 0;
for (int i = 0; i < 10; i++) {
sum0 += hx.read();
sum1 += hx.bias_read();
}
Serial.print(sum0/10);
Serial.print(" ");
Serial.println(sum1/10);
}

Arduino
17
17
-24961.30 0.31
-25042.10 0.06
-24981.30 0.20
37749.10 200.51
37719.40 200.24
37597.30 199.95
37582.50 199.87
37647.40 200.08
69114.80 299.76
69011.80 300.44
68868.80 300.50
100459.30 399.97
100351.60 400.16
100325.80 400.23
131672.59 500.06
131693.40 500.40
131663.29 500.29
37689.80 199.87
37639.10 200.10
~ 114 ~

22020.40 150.24
22022.10 150.24
EXCEL 18
18 1
0.165714286
(0.165714286) 0.2

18

ADC

-24961.3

0.31

0.31

-25042.1

0.06

0.06

-24981.3

0.2

0.2

37749.1

200.51

200

0.51

37719.4

200.24

200

0.24

37597.3

199.95

200

-0.05

37582.5

199.87

200

-0.13

37647.4

200.08

200

0.08

69114.8

299.76

300

-0.24

10

69011.8

300.44

300

0.44

11

68868.8

300.5

300

0.5

12

100459.3

399.97

400

-0.03

13

100351.6

400.16

400

0.16

14

100325.8

400.23

400

0.23

15

131672.59

500.06

500

0.06

16

131693.4

500.4

500

0.4

17

131663.29

500.29

500

0.29

18

37689.8

199.87

200

-0.13

19

37639.1

200.1

200

0.1

20

22020.4

150.24

150

0.24

21

22022.1

150.24

150

0.24

22

252.5467

252.381

0.165714

~ 115 ~

600
500
400
300
200
100
0
-100

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

35

18 35

x
(0.165714286)
0.2

~ 116 ~

~ 117 ~

(Load Cell)(Load Cell)


36
Analog to Digital Converters (ADC)

19

36

0.0031977841 Analog
to Digital Converters (ADC)-13117.91613 Arduino
~ 118 ~

hx.set_offset(-13117.91613)-13117.91613
0
19 0 -61.72

19

ADC

-32433

-61.72

0.013333

-61.7333

-32463.9

-61.81

-0.07667

-32427.6

-61.67

0.063333

-1108.5

38.41

0.1125

-1121.9

38.28

-0.0175

-1181.9

38.17

-0.1275

-1129.9

38.33

0.0325

30153.8

138.27

-0.09

30155.4

138.39

0.03

30175.4

138.44

0.08

30142.5

138.32

-0.04

30154.9

138.38

0.02

92688.9

338.59

0.16

92759.1

338.27

-0.16

92683.8

338.56

0.13

92716.9

338.21

-0.22

92688.6

338.52

0.09

123959.2

438.42

0.12

124046

438.5

0.2

123939.9

438.3

123906.6

438.26

-0.04

123918.5

438.09

-0.21

123937.7

438.35

0.05

100

200

400

500

38.2975

138.36

338.43

438.3

0.005217391

19
~ 119 ~


0.005217391 Analog to Digital Converters (ADC)
0

HX711 Arduino 20
void tare(int t)

20 HX711 Arduino
HX711(byte sck, byte dout, byte amp = 128, double co = 1); // sckdout
128 1
void set_amp(byte amp); // read()

bool is_ready(); // hx711 read()


long read(); // hx711

double bias_read(); // (read() - ) *


void tare(int t = 10); // read();
void set_co(double co = 1); // 1
void set_offset(long offset = 0); // 0
setup(){} void tare(int t );
// Analog to
Digital Converters (ADC)

HX711 (hx711_4)
#include <HX711.h> // HX711 Arduino library
~ 120 ~

HX711 (hx711_4)
#define datapin 8
//
#define clockpin 9 //
HX711 hx(clockpin, datapin,128,0.0031977841);
// HX711
void setup() {
hx.set_offset(-13117.91613);
Serial.begin(9600);
hx.tare(50);
// 50
}
void loop() {
delay(500);
double sum0 = 0;

double sum1 = 0;
for (int i = 0; i < 10; i++) {
sum0 += hx.read();
sum1 += hx.bias_read();
}
Serial.print((int)sum0/10); //
Serial.print(" ");
Serial.println((int)sum1/10);
//
}
21 Analog to
Digital Converters (ADC)

21

ADC

-1398

-1373

-1333

-1374

100

-2820

100

100

-2765

100

100

-2829

100

~ 121 ~

100

-2817

100

100

-2827

100

200

2297

200

200

2238

200

200

2295

200

300

819

300

300

789

300

300

777

300

400

-684

400

400

-706

400

400

-758

400

400

-723

400

500

-2198

500

500

-2267

500

500

-2243

500

LCD
Arduino sketcch
( 4)

Arduino LCD 1602 LCD


1602 HD447806 2x16 LCD
16
()
LED LED

Hitachi HD44780 LCD controller is one of the most common dot matrix liquid crystal display

(LCD) display controllers available. Hitachi developed the microcontroller specifically to drive
alphanumeric LCD display with a simple interface that could be connected to a general purpose
microcontroller or microprocessor
~ 122 ~

37 LCD 1602
16 22

37 LCD1602

22 LCD1602

Ground (0V)

(0V)

Supply voltage; 5V (4.7V 5.3V)

(+5V)

Contrast adjustment;
variable resistor

Selects command register when low; Register Select:


1: D0 D7
and data register when high
0: D0 D7

Low to write to the register; High to Read/Write mode:


1: LCD
read from the register
0: LCD

through

a (0-5V), 1k

LCD
~ 123 ~

I/O

Sends data to data pins when a high


Enable
to low pulse is given

8-bit data pins

Bit 0 LSB

Bit 1

Bit 2

10

Bit 3

11

Bit 4

12

Bit 5

13

Bit 6

14
15

Backlight VCC (5V)

Bit 7 MSB
( 330 R )

16

Backlight Ground (0V)

(GND)

(Guangzhou_Tinsharp_Industrial_Corp._Ltd., 2013)

LCD1602
(Guangzhou_Tinsharp_Industrial_Corp._Ltd., 2013) 38
LCD 1602 Arduino 39 LCD 1602
Arduino LCD 1602
LCD 1602 LiquidCrystal Library
Arduino LiquidCrystal Library
http://arduino.cc/en/Reference/LiquidCrystal LCD 1602
LCD 1602
(Guangzhou_Tinsharp_Industrial_Corp._Ltd., 2013)

LCD 1602 4-bit 8-bit 4-bit


I/O 4 (D4-D7)D0-D3
LCD 1602 4
4
~ 124 ~

38 LCD 1602
by Fritzing (Interaction_Design_Lab, 2013)

39 LCD 1602
by Fritzing (Interaction_Design_Lab, 2013)

~ 125 ~

Arduino LiquidCrystal Library (


Arduino LiquidCrystal Library
http://arduino.cc/en/Reference/LiquidCrystal) LCD 1602
Arduino LCD 1602
LiquidCrystal LCD 1602 (lcd1602_hello)

/*
LiquidCrystal Library - Hello World

Use a 16x2 LCD display The LiquidCrystal


library works with all LCD displays that are compatible with the
Hitachi HD44780 driver.
This sketch prints "Hello World!" to the LCD
and shows the time.
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//use db4-db7 as pin 5-2
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);

LCD 1602 LiquidCrystal

~ 126 ~

LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
1.

LiquidCrystal lcd ()

2.

LiquidCrystal(rs, enable, d4, d5, d6, d7)

LiquidCrystal(rs, enable, d0, d1, d2, d3,d4, d5, d6, d7)

LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)

LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)

LiquidCrystal.begin(16, 2)
1.

lcd ()

2.

LiquidCrystal.begin(16, 2)
lcd 16

LiquidCrystal.setCursor(0, 1)
1.

LiquidCrystal.setCursor(,) 0 , 0

2.

LiquidCrystal.setCursor(0, 1)
16

LiquidCrystal.print()
1.

LiquidCrystal.print () char, byte, int, long, or string

~ 127 ~

2.

lcd.print("hello, world!");
hello, world!

LiquidCrystal.autoscroll()
1.

lcd

2.

lcd.autoscroll();
lcd.print(thisChar);
16
LiquidCrystal - Text Direction
print()

LiquidCrystal.noAutoscroll()
1.

lcd

2.

lcd.noAutoscroll();
lcd.print(thisChar);
16
print()
16

~ 128 ~

LiquidCrystal.blink()
1.

lcd

2.

lcd.blink();
lcd

LiquidCrystal.noBlink()
1.

lcd

2.

lcd.noBlink ();
lcd

LiquidCrystal.cursor()
1.

lcd

2.

lcd.cursor();
lcd

LiquidCrystal.clear()
1.

lcd

2.

~ 129 ~

lcd.clear();
lcd
LiquidCrystal.home()
1.

lcd

2.

lcd.home();
lcd

LCD KeyPad Shield


Arduino sketcch
( 4)

Arduino 40 LCD
1602 button LCD

KeyPad LCD

(DFRobot, 2013)

~ 130 ~

40 LCD Keypad Shield


(DFRobot, 2013)

Arduino 40 LCD
1602 (buttons) LCD Keypad Shield (DFRobot,
2013) LCD

~ 131 ~

41 LCD Keypad Shield PCB Layout


(DFRobot, 2013)

41 LCD Keypad Shield 40


23

23 LCD Keypad Shield


Keypad

Arduino

D4

Arduino digital output pin 4

D5

Arduino digital output pin 5

D6

Arduino digital output pin 6

D7

Arduino digital output pin 7

RS

Arduino digital output pin 8

Register Selections

Enable

Arduino digital output pin 9

ENABLE SIGNAL

A0

Arduino Analog input pin 0

Key buttons ()

5V

Arduino pin 5V

5V

GND

Arduino pin Gnd

LCD 1602

~ 132 ~

keypad shield keypad shield


LCD 1602 (Buttons) 42
keypad shield

KeyPads Shield (keypad_keytest1)


//Using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// LiquidCrystal(rs, enable, d4, d5, d6, d7)
// LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
// LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed
reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
~ 133 ~

KeyPads Shield (keypad_keytest1)

}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("Push the buttons"); // print the message
}
void loop()
{
lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
lcd.print(millis()/1000); // display seconds elapsed since power-up
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) {
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:

{
lcd.print("UP ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
~ 134 ~

KeyPads Shield (keypad_keytest1)

break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}

42 keypadshield

LCD
Arduino sketcch
( 4)

24 LCD Keypad Shield Arduino


~ 135 ~

24

24 LCD Keypad Shield Arduino


Keypad

Arduino

D4

Arduino digital output pin 4

D5

Arduino digital output pin 5

D6

Arduino digital output pin 6

D7

Arduino digital output pin 7

RS

Arduino digital output pin 8

Register Selections

Enable

Arduino digital output pin 9

ENABLE SIGNAL

A0

Arduino Analog input pin 0

Key buttons ()

5V

Arduino pin 5V

5V

GND

Arduino pin Gnd

LCD 1602

HX711 ( 11 & 12)


Arduino 25 HX711 Arduino
25

25 HX711 Arduino
HX711

Arduino

Do/Rx

Arduino digital output pin 10

Do/Rx

Clock

Arduino digital output pin 11

Ck/Tx

Vcc

Arduino pin 5V

5V

GND

Arduino pin Gnd

(Load Cell)HX711 Arduino


Arduino sketch
(hx711_5)

#include <HX711.h> // HX711 Arduino library


#include <LiquidCrystal.h>
~ 136 ~

(hx711_5)
#define datapin 10
//
#define clockpin 11 //
HX711 hx(clockpin, datapin,128,0.0031977841);
// HX711
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed
reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup() {
hx.set_offset(-13117.91613);
Serial.begin(9600);
hx.tare(50); // 50
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
// lcd.print("Push the buttons"); // print a simple message
}
void loop() {
int key ;

delay(500);

double sum0 = 0;

double sum1 = 0;
~ 137 ~

(hx711_5)

key = checkkey();
if (key == 11)
{
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("tare .........");
hx.tare(50); // 50
delay(1000);
}
for (int i = 0; i < 10; i++) {
sum0 += hx.read();
sum1 += hx.bias_read();
}
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("ADC :"); // display adc value
lcd.print((int)sum0/10); // display adc value
lcd.print(" "); // display adc value
lcd.print(key); // display adc value
lcd.setCursor(0,1); // move to the begining of the second line
lcd.print((int)sum1/10); //
lcd.print(" g "); //
lcd.print(sum1/10); //
lcd.print(" g"); //
Serial.print(sum0/10); //
Serial.print(" ");

Serial.println((int)sum1/10); //
// checkkey();
}
int checkkey()
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) {
case btnRIGHT:
{
return 1 ;
break;
~ 138 ~

(hx711_5)

}
case btnLEFT:
{
return 2 ;
break;
}
case btnUP:
{
return 3 ;
break;
}
case btnDOWN:
{
return 4 ;
break;
}
case btnSELECT:
{
return 11 ;
break;
}
case btnNONE:
{
return 0 ;
break;
}

} }
Arduino
43 Keypad Shield
LCD 1602 Select
(Tare)

~ 139 ~

43

(Analog Sensor)

(Analog Sensor) Arduino 10


(Analog to Digital Convert)
24 (Analog to Digital Convert)
HX711(, 2013)
HX711 ( 11 12)
(Analog Sensor)
dea314@gmail.com (dea314@gmail.com, 2013)
Arduino HX711(AD Convert)

~ 140 ~

~ 141 ~

~ 142 ~

~ 143 ~

(Internal

Clock)

Arduino

Arduino (Tx/Rx),
Arduino http://arduino.cc/en/Reference/SoftwareSerial
Arduoino
Arduino

Arduino SoftwareSerial Library


1.
2.

Mega and Mega 2560


RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8
(62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

~ 144 ~

3.

Leonardo
RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

(Uart_test1)
/*
Software serial multple serial test
The circuit: * RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53,
62, 63, 64, 65, 66, 67, 68, 69
Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15
(SCK), 16 (MOSI).
*/
#include <SoftwareSerial.h>
#define RXPin 5
#define TXPin 6
SoftwareSerial mySerial(RXPin, TXPin); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
~ 145 ~

Arduino (Uart)
(Crowe, et al., 1988, Gulick, et al., 1990, Gulick, et al., 1993, Michael, 1989,
Michael, 1993, Michael, 1995, Michael, 1996, Michael, 1999, Story, et al., 1998,
Takahashi and Ibuka, 1998)(Method)
SoftwareSerial(rxPin, txPin))
SoftwareSerial mySerial =

SoftwareSerial(rxPin,

txPin);( Uart mySerial,(rxPin, txPin)

SoftwareSerial(rxPin, txPin)
SoftwareSerial.begin(speed)
(baud rate) 300, 600, 1200, 2400, 4800, 9600,
14400, 19200, 28800, 31250, 38400, 57600, and 115200 9600 bps(bit per
second)

SoftwareSerial.begin(9600)
9600 bps( 9600 )
SoftwareSerial.available()
(Rx)(Bytes)

if (SoftwareSerial: available() > 0)


data =

//

SoftwareSerial.read() ;

//

endif
,(
~ 146 ~

)
SoftwareSerial.isListening()

if (SoftwareSerial: isListening > 0)

//

Serial.println("Port One is listening!"); //


endif

SoftwareSerial.print(data)
SoftwareSerial.println(data)
Tx

SoftwareSerial: println("Hello World:) ;

// Hello World

Hello World Tx
SoftwareSerial.read()
(Bytes)

if (SoftwareSerial: available() > 0)


data =

//

SoftwareSerial.read() ;

//

endif
(Bytes)
SoftwareSerial.write(Byte Data)
(Bytes)(Tx)

SoftwareSerial.write(byte) ;

// byte (Tx)

endif
~ 147 ~

(Bytes)(Tx)
SoftwareSerial.peek()
(Rx)(Bytes)
peek()

if (SoftwareSerial: available() > 0)


data =

//

SoftwareSerial.peek() ;

//

endif
(Rx)(Bytes)
peek()
SoftwareSerial.overflow()
true
( false)

if (SoftwareSerial.overflow ())

//

Serial.println("SoftwareSerial overflow!");

//

endif
true
( false)

Uart_test1 & HX711_5


HX711_6 ()USB TTL ( 44) Arduino
~ 148 ~


Com277(
)(USB TTL ( 44))
putty8 (Tatham, 2013) ( Simon
Tatham

).

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
45 45 Arduino

HX711_6( Tx/Rx )
#include <HX711.h> // HX711 Arduino library
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define datapin 10
//
#define clockpin 11 //
#define RXPin 2
// Rx pin
#define TXPin 3
// Tx pin
HX711 hx(clockpin, datapin,128,0.0031977841);

// HX711

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


SoftwareSerial mySerial(RXPin, TXPin); // RX, TX
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons()
7

PuTTY TelnetSSHrlogin TCP PuTTY

Simon Tatham MIT licence


~ 149 ~

HX711_6( Tx/Rx )
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for
speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
return btnNONE; // when all others fail, return this...
}
void setup() {
hx.set_offset(-13117.91613);
Serial.begin(9600);
mySerial.begin(9600);
hx.tare(50); // 50
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
// lcd.print("Push the buttons"); // print a simple message
}
void loop() {
int key ;
delay(500);
double sum0 = 0;
double sum1 = 0;
key = checkkey();
if (key == 11)
{
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("tare .........");
hx.tare(50); // 50
delay(1000);
}
for (int i = 0; i < 10; i++) {
sum0 += hx.read();
~ 150 ~

HX711_6( Tx/Rx )
sum1 += hx.bias_read();
}
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("ADC :"); // display adc value
lcd.print((int)sum0/10); // display adc value
lcd.print(" "); // display adc value
lcd.print(key); // display adc value
lcd.setCursor(0,1); // move to the begining of the second line
lcd.print((int)sum1/10); //
lcd.print(" g "); //
lcd.print(sum1/10); //
lcd.print(" g"); //
Serial.print(sum0/10); //
Serial.print(" ");
Serial.println((int)sum1/10); //
mySerial.print(sum0/10); //
mySerial.print(" ");
mySerial.println((int)sum1/10);

//

// checkkey();
}
int checkkey()
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an
action
{
case btnRIGHT:
{
return 1 ;
break;
}
case btnLEFT:
{
return 2 ;
break;
}
~ 151 ~

HX711_6( Tx/Rx )
case btnUP:
{
return 3 ;
break;
}
case btnDOWN:
{
return 4 ;
break;
}
case btnSELECT:
{
return 11 ;
break;
}
case btnNONE:
{
return 0 ;
break;
}
}
}

~ 152 ~

44 USB TTL

45

45

~ 153 ~

HX711_7 newflag

oldWht

HX711_7( Tx/Rx )
#include <HX711.h> // HX711rduino library
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define datapin 10
#define clockpin 11
#define RXPin 2
// Rx pin
#define TXPin 3
// Tx pin
//------define key button -----#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
//------define key button -----int lcd_key = 0 ;
int adc_key_in = 0 ;
double sum0 = 0 ;
double sum1 = 0 ;
int newflag = 0 ;
int oldWht = 0 ;
HX711 hx(clockpin, datapin,128,0.0031977841);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
~ 154 ~

HX711_7( Tx/Rx )
SoftwareSerial mySerial(RXPin, TXPin); // RX, TX

void setup() {
// int oldWht;
hx.set_offset(-13117.91613);
Serial.begin(9600);
mySerial.begin(9600);
hx.tare(50);
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
// lcd.print("Push the buttons");
// print a simple message
sum0 = 0;
sum1 = 0;
for (int i = 0; i < 10; i++)
{
sum0 += hx.read();
sum1 += hx.bias_read();
}
oldWht = sum1/10 ;
newflag = 0 ;
}
void loop() {
int tmpno = 0 ;
int key ;
key = checkkey();
if (key == 11)
{
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("tare .........");
hx.tare(50);
delay(1000);
}
~ 155 ~

HX711_7( Tx/Rx )
sum0 = 0;
sum1 = 0;
for (int i = 0; i < 10; i++)
{
sum0 += hx.read();
sum1 += hx.bias_read();
}
tmpno = (int)sum1/10 ;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ADC :");
lcd.print((int)sum0/10);
lcd.print(" ");
lcd.print(key);
lcd.setCursor(0,1);
lcd.print((int)sum1/10); \
lcd.print(" g ");
lcd.print(sum1/10);
lcd.print(" g");
Serial.print(sum0/10);
Serial.print(" ");
Serial.println((int)sum1/10);
if( (tmpno> 0) && (tmpno != oldWht) )
{
newflag = 1 ;
}
else
{
newflag = 0 ;
}
if ( newflag == 1)
{
mySerial.print(tmpno) ;
mySerial.println("g") ;
oldWht = tmpno ;
}
~ 156 ~

HX711_7( Tx/Rx )
// checkkey();
delay(500);
}
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for
speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
return btnNONE; // when all others fail, return this...
}

int checkkey()
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an
action
{
case btnRIGHT:
{
return 1 ;
break;
}
case btnLEFT:
{
return 2 ;
break;
}
case btnUP:
~ 157 ~

HX711_7( Tx/Rx )
{
return 3 ;
break;
}
case btnDOWN:
{
return 4 ;
break;
}
case btnSELECT:
{
return 11 ;
break;
}
case btnNONE:
{
return 0 ;
break;
}
}
}
46

~ 158 ~

46

Arduino

Arduino (Internal Clock)


Arduino Tiny RTC I2C 47
Tiny RTC I2C DS1307
DS1307 (Jeelab, 2013) Tiny RTC
I2C (DS1307_test1) Arduino

~ 159 ~

47 Tiny RTC I2C

DS1307_test1(Tiny RTC I2C )


// Date and time functions using a DS1307 RTC connected via I2C and Wire
lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
Serial.print("System Date Time:(");
Serial.print(__DATE__);
Serial.print("^");
Serial.print(__TIME__);
Serial.println(")");
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
Serial.print("(");
Serial.print(__DATE__);
Serial.print("^");
Serial.print(__TIME__);
Serial.println(")");
// following line sets the RTC to the date & time this sketch was compiled
~ 160 ~

DS1307_test1(Tiny RTC I2C )


RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
~ 161 ~

DS1307_test1(Tiny RTC I2C )


Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
Arduino
( Arduino
)

(HX711_8) 48

(Rx/Tx) RS232 (Rx/Tx) RS485

HX711_8()
#include <HX711.h> // HX711rduino library
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include "RTClib.h"
~ 162 ~

HX711_8()
#define datapin 10
#define clockpin 11
#define RXPin 2
// Rx pin
#define TXPin 3
// Tx pin
//------define key button -----#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
//------define key button -----int lcd_key = 0 ;
int adc_key_in = 0 ;
double sum0 = 0 ;
double sum1 = 0 ;
int newflag = 0 ;
int oldWht = 0 ;
char datetimestr1[24];
HX711 hx(clockpin, datapin,128,0.0031977841);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SoftwareSerial mySerial(RXPin, TXPin); // RX, TX
RTC_DS1307 RTC;
//init RTC
void setup() {
// int oldWht;
hx.set_offset(-13117.91613);
Serial.begin(9600);
mySerial.begin(9600);
Wire.begin();
// ds1307

RTC.begin();
// lauch RTC
hx.tare(50);
lcd.begin(16, 2); // start the library
~ 163 ~

HX711_8()
lcd.setCursor(0,0);
sum0 = 0;
sum1 = 0;
oldWht = 0 ;
//
newflag = 0 ;
//

}
void loop() {
int tmpno = 0 ;
int key ;
key = checkkey();
if (key == 11)
{
lcd.clear();
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("tare .........");
hx.tare(50);
oldWht = 0 ;
newflag = 0 ;
delay(200);
}
sum0 = 0;
sum1 = 0;
for (int i = 0; i < 10; i++)
{
sum0 += hx.read();
sum1 += hx.bias_read();
}
tmpno = (int)sum1/10 ;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ADC :");
lcd.print((int)sum0/10);
lcd.print(" ");
lcd.print(key);
lcd.setCursor(0,1);
~ 164 ~

HX711_8()
lcd.print((int)sum1/10); \
lcd.print(" g ");
lcd.print(sum1/10);
lcd.print(" g");
Serial.print(getdatetime()) ;
Serial.print(sum0/10);
Serial.print(" ");
Serial.println((int)sum1/10);
if( (tmpno> 0) && (tmpno != oldWht) )
{
newflag = 1 ;
}
else
{
newflag = 0 ;
}
if ( newflag == 1)
{
mySerial.print(getdatetime()) ;
mySerial.print(tmpno) ;
mySerial.println("g") ;
oldWht = tmpno ;
}
// checkkey();
delay(500);
}
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for
speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
~ 165 ~

HX711_8()
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
return btnNONE; // when all others fail, return this...
}

int checkkey()
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an
action
{
case btnRIGHT:
{
return 1 ;
break;
}
case btnLEFT:
{
return 2 ;
break;
}
case btnUP:
{
return 3 ;
break;
}
case btnDOWN:
{
return 4 ;
break;
}
case btnSELECT:
{
return 11 ;
break;
}
~ 166 ~

HX711_8()
case btnNONE:
{
return 0 ;
break;
}
}
}
String getdatetime()
{
//
ds1307

DateTime now = RTC.now();


String tmp ="";
tmp.concat(now.year()) ;
tmp.concat('/') ;
tmp.concat(now.month());
tmp.concat('/');
tmp.concat(now.day());
tmp.concat(' ');
tmp.concat(now.hour());
tmp.concat(':');
tmp.concat(now.minute());
tmp.concat(':');
tmp.concat(now.second());
tmp.concat("
");
return tmp ;
}

~ 167 ~

48

Arduino

Arduino
~ 168 ~

Arduino

~ 169 ~


(Yung-Chung Tsao)

Email:prgbruce@gmail.com

(Chih-Cheng Hsu)(UCLA)
IBM

Email: khsu@mgt.ncu.edu.tw

(Yin-Te Tsai)

Email:yttsai@pu.edu.tw

~ 170 ~


HX711 Arduino
HX711 Arduino librarydea314@gmail.com Google Project
Platform9(dea314@gmail.com, 2013) hx711-Arduino-v0.01

HX711

library

for

Arduino

platform

http://code.google.com/p/hx711-arduino/downloads/list?q=label:Featured :
dea314@gmail.com

hx711.cpp
#include <Arduino.h>
#include <HX711.h>
HX711::HX711(byte sck, byte dout, byte amp, double co) {
SCK = sck;
DOUT = dout;
set_amp(amp);
COEFFICIENT = co;
pinMode(SCK, OUTPUT);
pinMode(DOUT, INPUT);
digitalWrite(SCK, LOW);
read();
}
void HX711::set_amp(byte amp) {
switch (amp) {
case 32: AMP = 2; break;
case 64: AMP = 3; break;
case 128: AMP = 1; break;
9

http://code.google.com/,Project Hosting on Google Code

~ 171 ~

hx711.cpp
}
}
bool HX711::is_ready() {
return digitalRead(DOUT) == LOW;
}
long HX711::read() {
long val = 0;
while (!is_ready());
for (int i = 0; i < 24; i++) {
pulse(SCK);
val <<= 1;
if (digitalRead(DOUT) == HIGH) val++;
}
for (int i = 0; i < AMP; i++) {
pulse(SCK);
}
return val & (1L << 23) ? val | ((-1L) << 24) : val;
}
double HX711::bias_read() {
return (read() - OFFSET) * COEFFICIENT;
}
void HX711::tare(int t) {
double sum = 0;
for (int i = 0; i < t; i++) {
sum += read();
}
set_offset(sum / t);
}
void HX711::set_offset(long offset) {
OFFSET = offset;
}
void HX711::set_co(double co) {
~ 172 ~

hx711.cpp
COEFFICIENT = co;
}

hx711.h
/*
* ---------------------------------------------------------------------------* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice
you
* can do whatever you want with this stuff. If we meet some day, and you
think
* this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
* ---------------------------------------------------------------------------*/
#ifndef HX711_H
#define HX711_H
#define pulse(pin) { digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
#include <Arduino.h>
class HX711
~ 173 ~

hx711.h
{
private:
byte SCK;
byte DOUT;
byte AMP;
long OFFSET;
double COEFFICIENT;
public:
// define sck , dout pin, amplification factor and coefficient
HX711(byte sck, byte dout, byte amp = 128, double co = 1);
// set amplification factor, take effect after one call to read()
void set_amp(byte amp);
// test hx711 is ready or not, will be called in read()
bool is_ready();
// return difference votage, will be blocked if hx711 is not ready
long read();
// return (read() - offset) * coefficient
double bias_read();
// set no-load value to offset, euqla to average of t times read();
void tare(int t = 10);
// set coefficient
void set_co(double co = 1);
// set offset
void set_offset(long offset = 0);
};
#endif

~ 174 ~

LCD 1602
LCD 1602 Adafruit Industries (Adafruit_Industries, 2013)
Basic 16x2 LCD with Arduino library Adafruit Industries

LiquidCrystal.cpp

#include "LiquidCrystal.h"

//
DL = 1; 8-bit interface data
//
N = 0; 1-line display
//
F = 0; 5x8 dot character font
// 3. Display on/off control:
//
D = 0; Display off
//
C = 0; Cursor off
//
B = 0; Blinking off
// 4. Entry mode set:
//
I/D = 1; Increment by 1
//
S = 0; No shift

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// When the display powers up, it is configured as follows:
//
// 1. Display clear
// 2. Function set:

LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,

uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,


~ 175 ~

LiquidCrystal.cpp

uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)


{
init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
}
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
{
init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
}
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
{
init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
}
LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
{
init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
}
LiquidCrystal::LiquidCrystal(uint8_t i2caddr) {
_i2cAddr = i2caddr;
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
// the I/O expander pinout
_rs_pin = 1;
_rw_pin = 255;
_enable_pin = 2;
_data_pins[0] = 3; // really d4
_data_pins[1] = 4; // really d5
_data_pins[2] = 5; // really d6
_data_pins[3] = 6; // really d7
// we can't begin() yet :(
}
~ 176 ~

LiquidCrystal.cpp

LiquidCrystal::LiquidCrystal(uint8_t data, uint8_t clock, uint8_t latch ) {


_i2cAddr = 255;
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
// the SPI expander pinout
_rs_pin = 1;
_rw_pin = 255;
_enable_pin = 2;
_data_pins[0] = 6; // really d4
_data_pins[1] = 5; // really d5
_data_pins[2] = 4; // really d6
_data_pins[3] = 3; // really d7
_SPIdata = data;
_SPIclock = clock;
_SPIlatch = latch;
pinMode(_SPIdata, OUTPUT);
pinMode(_SPIclock, OUTPUT);
pinMode(_SPIlatch, OUTPUT);
_SPIbuff = 0;
// we can't begin() yet :(
begin(16,1);
}
void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t

enable,

uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,


uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)

{
_rs_pin = rs;
_rw_pin = rw;
_enable_pin = enable;
_data_pins[0] = d0;
_data_pins[1] = d1;
_data_pins[2] = d2;
_data_pins[3] = d3;
_data_pins[4] = d4;
_data_pins[5] = d5;
_data_pins[6] = d6;
~ 177 ~

LiquidCrystal.cpp

_data_pins[7] = d7;
_i2cAddr = 255;
_SPIclock = _SPIdata = _SPIlatch = 255;
pinMode(_rs_pin, OUTPUT);
// we can save 1 pin by not using RW. Indicate by passing 255 instead

of pin#

if (_rw_pin != 255) {

pinMode(_rw_pin, OUTPUT);

pinMode(_enable_pin, OUTPUT);

if (fourbitmode)

_displayfunction = LCD_4BITMODE | LCD_1LINE |


LCD_5x8DOTS;

else

_displayfunction = LCD_8BITMODE | LCD_1LINE |


LCD_5x8DOTS;

begin(16, 1);
}
void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {

// check if i2c

if (_i2cAddr != 255) {

_i2c.begin(_i2cAddr);

_i2c.pinMode(7, OUTPUT); // backlight

_i2c.digitalWrite(7, HIGH); // backlight

for (uint8_t i=0; i<4; i++)

_pinMode(_data_pins[i], OUTPUT);
_i2c.pinMode(_rs_pin, OUTPUT);
_i2c.pinMode(_enable_pin, OUTPUT);
} else if (_SPIclock != 255) {
_SPIbuff = 0x80; // backlight
}
if (lines > 1) {
_displayfunction |= LCD_2LINE;
}
_numlines = lines;
_currline = 0;
// for some 1 line displays you can select a 10 pixel high font
~ 178 ~

LiquidCrystal.cpp

if ((dotsize != 0) && (lines == 1)) {


_displayfunction |= LCD_5x10DOTS;
}
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40ms after power rises

above 2.7V

// before sending commands. Arduino can turn on way befer 4.5V so


we'll wait 50

delayMicroseconds(50000);

// Now we pull both RS and R/W low to begin commands

_digitalWrite(_rs_pin, LOW);

_digitalWrite(_enable_pin, LOW);

if (_rw_pin != 255) {

_digitalWrite(_rw_pin, LOW);

//put the LCD into 4 bit or 8 bit mode

if (! (_displayfunction & LCD_8BITMODE)) {

// this is according to the hitachi HD44780 datasheet

// figure 24, pg 46

// we start in 8bit mode, try to set 4 bit mode

write4bits(0x03);

delayMicroseconds(4500); // wait min 4.1ms

// second try

write4bits(0x03);

delayMicroseconds(4500); // wait min 4.1ms

// third go!
write4bits(0x03);
delayMicroseconds(150);
// finally, set to 8-bit interface
write4bits(0x02);
} else {
// this is according to the hitachi HD44780 datasheet
// page 45 figure 23
// Send function set command sequence
command(LCD_FUNCTIONSET | _displayfunction);
delayMicroseconds(4500); // wait more than 4.1ms
// second try
~ 179 ~

LiquidCrystal.cpp

command(LCD_FUNCTIONSET | _displayfunction);
delayMicroseconds(150);
// third go
command(LCD_FUNCTIONSET | _displayfunction);
}
// finally, set # lines, font size, etc.
command(LCD_FUNCTIONSET | _displayfunction);
// turn the display on with no cursor or blinking default
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF |

LCD_BLINKOFF;

display();
// clear it off

clear();
// Initialize to default text direction (for romance languages)

_displaymode = LCD_ENTRYLEFT |
LCD_ENTRYSHIFTDECREMENT;

// set the entry mode

command(LCD_ENTRYMODESET | _displaymode);
}
/********** high level commands, for the user! */
void LiquidCrystal::clear()
{

command(LCD_CLEARDISPLAY); // clear display, set cursor


posi-tion to zero

delayMicroseconds(2000);

// this command takes a long time!

}
void LiquidCrystal::home()
{
command(LCD_RETURNHOME); // set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}
void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
{
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row > _numlines ) {
row = _numlines-1;
// we count rows starting w/0
}
~ 180 ~

LiquidCrystal.cpp

}
// Turn the display on/off (quickly)
void LiquidCrystal::noDisplay() {
_displaycontrol &= ~LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal::display() {
_displaycontrol |= LCD_DISPLAYON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turns the underline cursor on/off
void LiquidCrystal::noCursor() {
_displaycontrol &= ~LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal::cursor() {
_displaycontrol |= LCD_CURSORON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turn on and off the blinking cursor
void LiquidCrystal::noBlink() {
_displaycontrol &= ~LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}

void LiquidCrystal::blink() {
_displaycontrol |= LCD_BLINKON;
command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// These commands scroll the display without changing the RAM
void LiquidCrystal::scrollDisplayLeft(void) {
command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE |

command(LCD_SETDDRAMADDR | (col + row_offsets[row]));

LCD_MOVELEFT);
}
void LiquidCrystal::scrollDisplayRight(void) {

command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE |
LCD_MOVERIGHT);
~ 181 ~

LiquidCrystal.cpp

}
// This is for text that flows Left to Right
void LiquidCrystal::leftToRight(void) {
_displaymode |= LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This is for text that flows Right to Left
void LiquidCrystal::rightToLeft(void) {
_displaymode &= ~LCD_ENTRYLEFT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'right justify' text from the cursor
void LiquidCrystal::autoscroll(void) {
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'left justify' text from the cursor
void LiquidCrystal::noAutoscroll(void) {
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
command(LCD_ENTRYMODESET | _displaymode);
}
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
location &= 0x7; // we only have 8 locations 0-7
command(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++) {
write(charmap[i]);
}
}
/*********** mid level commands, for sending data/cmds */
inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW);
}
#if ARDUINO >= 100
inline size_t LiquidCrystal::write(uint8_t value) {
send(value, HIGH);
~ 182 ~

LiquidCrystal.cpp

}
#else
inline void LiquidCrystal::write(uint8_t value) {
send(value, HIGH);
}
#endif
/************ low level data pushing commands **********/
// little wrapper for i/o writes
void LiquidCrystal::_digitalWrite(uint8_t p, uint8_t d) {
if (_i2cAddr != 255) {
// an i2c command
_i2c.digitalWrite(p, d);
} else if (_SPIclock != 255) {
if (d == HIGH)
_SPIbuff |= (1 << p);
else
_SPIbuff &= ~(1 << p);
digitalWrite(_SPIlatch, LOW);
shiftOut(_SPIdata, _SPIclock, MSBFIRST,_SPIbuff);
digitalWrite(_SPIlatch, HIGH);
} else {
// straightup IO
digitalWrite(p, d);
}

}
// Allows to set the backlight, if the LCD backpack is used
void LiquidCrystal::setBacklight(uint8_t status) {
// check if i2c or SPI
if ((_i2cAddr != 255) || (_SPIclock != 255)) {
_digitalWrite(7, status); // backlight is on pin 7
}
}
// little wrapper for i/o directions
void LiquidCrystal::_pinMode(uint8_t p, uint8_t d) {
if (_i2cAddr != 255) {
// an i2c command

return 1;

~ 183 ~

LiquidCrystal.cpp

_i2c.pinMode(p, d);
} else if (_SPIclock != 255) {
// nothing!
} else {
// straightup IO
pinMode(p, d);
}
}
// write either command or data, with automatic 4/8-bit selection
void LiquidCrystal::send(uint8_t value, uint8_t mode) {
_digitalWrite(_rs_pin, mode);
// if there is a RW pin indicated, set it low to Write
if (_rw_pin != 255) {
_digitalWrite(_rw_pin, LOW);
}
if (_displayfunction & LCD_8BITMODE) {
write8bits(value);
} else {
write4bits(value>>4);
write4bits(value);
}
}
void LiquidCrystal::pulseEnable(void) {
_digitalWrite(_enable_pin, LOW);
delayMicroseconds(1);
_digitalWrite(_enable_pin, HIGH);
delayMicroseconds(1);
// enable pulse must be >450ns
_digitalWrite(_enable_pin, LOW);
delayMicroseconds(100);
// commands need > 37us to settle
}
void LiquidCrystal::write4bits(uint8_t value) {
if (_i2cAddr != 255) {
uint8_t out = 0;
out = _i2c.readGPIO();
// speed up for i2c since its sluggish
for (int i = 0; i < 4; i++) {
out &= ~_BV(_data_pins[i]);
~ 184 ~

LiquidCrystal.cpp

out |= ((value >> i) & 0x1) << _data_pins[i];


}
// make sure enable is low
out &= ~ _BV(_enable_pin);
_i2c.writeGPIO(out);
// pulse enable
delayMicroseconds(1);
out |= _BV(_enable_pin);
_i2c.writeGPIO(out);
delayMicroseconds(1);
out &= ~_BV(_enable_pin);
_i2c.writeGPIO(out);
delayMicroseconds(100);
} else {
for (int i = 0; i < 4; i++) {
_pinMode(_data_pins[i], OUTPUT);
_digitalWrite(_data_pins[i], (value >> i) & 0x01);
}
pulseEnable();
}
}
void LiquidCrystal::write8bits(uint8_t value) {
for (int i = 0; i < 8; i++) {
_pinMode(_data_pins[i], OUTPUT);
_digitalWrite(_data_pins[i], (value >> i) & 0x01);
}
pulseEnable();

LiquidCrystal.h

#ifndef LiquidCrystal_h
#define LiquidCrystal_h
#include <inttypes.h>
#include "Print.h"
#include "Adafruit_MCP23008.h"
~ 185 ~

LiquidCrystal.h

// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
class LiquidCrystal : public Print {
public:
LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
~ 186 ~

LiquidCrystal.h

uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);


LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
LiquidCrystal(uint8_t i2cAddr);
LiquidCrystal(uint8_t data, uint8_t clock, uint8_t latch);
void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
void begin(uint8_t cols, uint8_t rows, uint8_t charsize =

LCD_5x8DOTS);

void clear();

void home();

void noDisplay();

void display();

void noBlink();

void blink();

void noCursor();

void cursor();

void scrollDisplayLeft();

void scrollDisplayRight();

void leftToRight();

void rightToLeft();

void autoscroll();

void noAutoscroll();

// only if using backpack

void setBacklight(uint8_t status);

void createChar(uint8_t, uint8_t[]);

void setCursor(uint8_t, uint8_t);


#if ARDUINO >= 100

virtual size_t write(uint8_t);


#else

virtual void write(uint8_t);


~ 187 ~

LiquidCrystal.h

#endif
void command(uint8_t);
private:
void send(uint8_t, uint8_t);
void write4bits(uint8_t);
void write8bits(uint8_t);
void pulseEnable();
void _digitalWrite(uint8_t, uint8_t);
void _pinMode(uint8_t, uint8_t);
uint8_t _rs_pin; // LOW: command. HIGH: character.
uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD.
uint8_t _enable_pin; // activated by a HIGH pulse.
uint8_t _data_pins[8];
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _initialized;
uint8_t _numlines,_currline;
uint8_t _SPIclock, _SPIdata, _SPIlatch;
uint8_t _SPIbuff;
uint8_t _i2cAddr;
Adafruit_MCP23008 _i2c;
};
#endif

~ 188 ~

LCD&Keypad Shield
LCD&Keypad Shield Arduino

LCD4Bit_mod.cpp
/*
LCD4Bit
An arduino library for comms with HD44780-compatible LCD, in 4-bit mode
Sources:
- The original "LiquidCrystal" 8-bit library and tutorial
http://www.arduino.cc/en/uploads/Tutorial/LiquidCrystal.zip
http://www.arduino.cc/en/Tutorial/LCDLibrary
Usage:
see the examples folder of this library distribution.
*/
#include "LCD4Bit_mod.h"
extern "C" {
#include <stdio.h> //not needed yet
#include <string.h> //needed for strlen()
#include <inttypes.h>
#include "WConstants.h" //all things wiring / arduino
}
//command bytes for LCD
#define CMD_CLR 0x01
#define CMD_RIGHT 0x1C
#define CMD_LEFT 0x18
#define CMD_HOME 0x02
// --------- PINS ------------------------------------//is the RW pin of the LCD under our control? If we're only ever going to
write to the LCD, we can use one less microcontroller pin, and just tie the
LCD pin to the necessary signal, high or low.
//this stops us sending signals to the RW pin if it isn't being used.
int USING_RW = false;
//RS, RW and Enable can be set to whatever you like
int RS = 8;
~ 189 ~

LCD4Bit_mod.cpp
int RW = 11;
int Enable = 9;
//DB should be an unseparated group of pins - because of lazy coding in
pushNibble()
int DB[] = {4, 5, 6, 7}; //wire these to DB4~7 on LCD.
//-------------------------------------------------------int g_num_lines = 2;
//pulse the Enable pin high (for a microsecond).
//This clocks whatever command or data is in DB4~7 into the LCD controller.
void LCD4Bit_mod::pulseEnablePin(){
digitalWrite(Enable,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delay(1); // pause 1 ms. TODO: what delay, if any, is necessary here?
}
//push a nibble of data through the the LCD's DB4~7 pins, clocking with the
Enable pin.
//We don't care what RS and RW are, here.
void LCD4Bit_mod::pushNibble(int value){
int val_nibble= value & 0x0F; //clean the value. (unnecessary)
for (int i=DB[0]; i <= DB[3]; i++) {
digitalWrite(i,val_nibble & 01);
val_nibble >>= 1;
}
pulseEnablePin();
}
//push a byte of data through the LCD's DB4~7 pins, in two steps, clocking
each with the enable pin.
void LCD4Bit_mod::pushByte(int value){
int val_lower = value & 0x0F;
int val_upper = value >> 4;
pushNibble(val_upper);
pushNibble(val_lower);
}
~ 190 ~

LCD4Bit_mod.cpp
//stuff the library user might call--------------------------------//constructor. num_lines must be 1 or 2, currently.
LCD4Bit_mod::LCD4Bit_mod (int num_lines) {
g_num_lines = num_lines;
if (g_num_lines < 1 || g_num_lines > 2)
{
g_num_lines = 1;
}
}
void LCD4Bit_mod::commandWriteNibble(int nibble) {
digitalWrite(RS, LOW);
if (USING_RW) { digitalWrite(RW, LOW); }
pushNibble(nibble);
}
void LCD4Bit_mod::commandWrite(int value) {
digitalWrite(RS, LOW);
if (USING_RW) { digitalWrite(RW, LOW); }
pushByte(value);
//TODO: perhaps better to add a delay after EVERY command, here.
many need a delay, apparently.
}
//print the given character at the current cursor position. overwrites, doesn't
insert.
void LCD4Bit_mod::print(int value) {
//set the RS and RW pins to show we're writing data
digitalWrite(RS, HIGH);
if (USING_RW) { digitalWrite(RW, LOW); }
//let pushByte worry about the intricacies of Enable, nibble order.
pushByte(value);
}
void LCD4Bit_mod::printIn(char msg[]) {
uint8_t i;
for (i=0;i < strlen(msg);i++){
print(msg[i]);
}
}
~ 191 ~

LCD4Bit_mod.cpp
//send the clear screen command to the LCD
void LCD4Bit_mod::clear(){
commandWrite(CMD_CLR);
delay(1);
}
void LCD4Bit_mod::init () {
pinMode(Enable,OUTPUT);
pinMode(RS,OUTPUT);
if (USING_RW) { pinMode(RW,OUTPUT); }
pinMode(DB[0],OUTPUT);
pinMode(DB[1],OUTPUT);
pinMode(DB[2],OUTPUT);
pinMode(DB[3],OUTPUT);
delay(50);
commandWriteNibble(0x03);
delay(5);
commandWriteNibble(0x03);
delayMicroseconds(100);
commandWriteNibble(0x03);
delay(5);
// needed by the LCDs controller
//this being 2 sets up 4-bit mode.
commandWriteNibble(0x02);
commandWriteNibble(0x02);
int num_lines_ptn = g_num_lines - 1 << 3;
int dot_format_ptn = 0x00;
//5x7 dots. 0x04 is 5x10
commandWriteNibble(num_lines_ptn | dot_format_ptn);
delayMicroseconds(60);
commandWrite(0x0C);
delayMicroseconds(60);
//clear display
commandWrite(0x01);
delay(3);
commandWrite(0x06);
delay(1);//TODO: remove unnecessary delays
}
void LCD4Bit_mod::cursorTo(int line_num, int x){
~ 192 ~

LCD4Bit_mod.cpp
//first, put cursor home
commandWrite(CMD_HOME);
if (g_num_lines==1){
line_num = 1;
}
if (line_num == 2){
x += 40;
}
for (int i=0; i<x; i++) {
commandWrite(0x14);
}
}
void LCD4Bit_mod::leftScroll(int num_chars, int delay_time){
for (int i=0; i<num_chars; i++) {
commandWrite(CMD_LEFT);
delay(delay_time);
}
}

LCD4Bit_mod.h
#ifndef LCD4Bit_mod_h
#define LCD4Bit_mod_h
#include <inttypes.h>
class LCD4Bit_mod {
public:
LCD4Bit_mod(int num_lines);
void commandWrite(int value);
void init();
void print(int value);
void printIn(char value[]);
void clear();
void cursorTo(int line_num, int x);
void leftScroll(int chars, int delay_time);
void commandWriteNibble(int nibble);
~ 193 ~

LCD4Bit_mod.h
private:
void pulseEnablePin();
void pushNibble(int nibble);
void pushByte(int value);
};
#endif

~ 194 ~

DS1307
DS1307 Adafruit Industries(Jeelab, 2013) Tiny RTC
I2C library Adafruit Industries

RTClib.cpp (Tiny RTC I2C )


// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
#include <Wire.h>
#include <avr/pgmspace.h>
#include "RTClib.h"
#define DS1307_ADDRESS 0x68
#define SECONDS_PER_DAY 86400L
#define SECONDS_FROM_1970_TO_2000 946684800
#if (ARDUINO >= 100)
#include <Arduino.h> // capital A so it is error prone on case-sensitive
filesystems
#else
#include <WProgram.h>
#endif
int i = 0; //The new wire library needs to take an int when you are sending for
the zero register
const uint8_t daysInMonth [] PROGMEM =
{ 31,28,31,30,31,30,31,31,30,31,30,31 }; //has to be const or compiler
compaints
// number of days since 2000/01/01, valid for 2001..2099
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += pgm_read_byte(daysInMonth + i - 1);
if (m > 2 && y % 4 == 0)
++days;
~ 195 ~

RTClib.cpp (Tiny RTC I2C )


return days + 365 * y + (y + 3) / 4 - 1;
}
static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {
return ((days * 24L + h) * 60 + m) * 60 + s;
}
// DateTime implementation - ignores time zones and DST changes
// NOTE: also ignores leap seconds, see
http://en.wikipedia.org/wiki/Leap_second
DateTime::DateTime (uint32_t t) {
t -= SECONDS_FROM_1970_TO_2000;
// bring to 2000 timestamp
from 1970
ss = t % 60;
t /= 60;
mm = t % 60;
t /= 60;
hh = t % 24;
uint16_t days = t / 24;
uint8_t leap;
for (yOff = 0; ; ++yOff) {
leap = yOff % 4 == 0;
if (days < 365 + leap)
break;
days -= 365 + leap;
}
for (m = 1; ; ++m) {
uint8_t daysPerMonth = pgm_read_byte(daysInMonth + m - 1);
if (leap && m == 2)
++daysPerMonth;
if (days < daysPerMonth)
break;
days -= daysPerMonth;
}
d = days + 1;
}
DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
uint8_t min, uint8_t sec) {
~ 196 ~

RTClib.cpp (Tiny RTC I2C )


if (year >= 2000)
year -= 2000;
yOff = year;
m = month;
d = day;
hh = hour;
mm = min;
ss = sec;
}
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
// A convenient constructor for using "the compiler's time":
//
DateTime now (__DATE__, __TIME__);
// NOTE: using PSTR would further reduce the RAM footprint
DateTime::DateTime (const char* date, const char* time) {
// sample input: date = "Dec 26 2009", time = "12:34:56"
yOff = conv2d(date + 9);
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
switch (date[0]) {
case 'J': m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7; break;
case 'F': m = 2; break;
case 'A': m = date[2] == 'r' ? 4 : 8; break;
case 'M': m = date[2] == 'r' ? 3 : 5; break;
case 'S': m = 9; break;
case 'O': m = 10; break;
case 'N': m = 11; break;
case 'D': m = 12; break;
}
d = conv2d(date + 4);
hh = conv2d(time);
mm = conv2d(time + 3);
ss = conv2d(time + 6);
~ 197 ~

RTClib.cpp (Tiny RTC I2C )


}
uint8_t DateTime::dayOfWeek() const {
uint16_t day = date2days(yOff, m, d);
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
uint32_t DateTime::unixtime(void) const {
uint32_t t;
uint16_t days = date2days(yOff, m, d);
t = time2long(days, hh, mm, ss);
t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000
return t;
}
// RTC_DS1307 implementation
static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
uint8_t RTC_DS1307::begin(void) {
return 1;
}
#if (ARDUINO >= 100)
uint8_t RTC_DS1307::isrunning(void) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 1);
uint8_t ss = Wire.read();
return !(ss>>7);
}
void RTC_DS1307::adjust(const DateTime& dt) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
Wire.write(bin2bcd(dt.second()));
Wire.write(bin2bcd(dt.minute()));
Wire.write(bin2bcd(dt.hour()));
Wire.write(bin2bcd(0));
Wire.write(bin2bcd(dt.day()));
Wire.write(bin2bcd(dt.month()));
~ 198 ~

RTClib.cpp (Tiny RTC I2C )


Wire.write(bin2bcd(dt.year() - 2000));
Wire.write(i);
Wire.endTransmission();
}
DateTime RTC_DS1307::now() {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
uint8_t ss = bcd2bin(Wire.read() & 0x7F);
uint8_t mm = bcd2bin(Wire.read());
uint8_t hh = bcd2bin(Wire.read());
Wire.read();
uint8_t d = bcd2bin(Wire.read());
uint8_t m = bcd2bin(Wire.read());
uint16_t y = bcd2bin(Wire.read()) + 2000;
return DateTime (y, m, d, hh, mm, ss);
}
#else
uint8_t RTC_DS1307::isrunning(void) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 1);
uint8_t ss = Wire.receive();
return !(ss>>7);
}
void RTC_DS1307::adjust(const DateTime& dt) {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(i);
Wire.send(bin2bcd(dt.second()));
Wire.send(bin2bcd(dt.minute()));
Wire.send(bin2bcd(dt.hour()));
Wire.send(bin2bcd(0));
Wire.send(bin2bcd(dt.day()));
Wire.send(bin2bcd(dt.month()));
~ 199 ~

RTClib.cpp (Tiny RTC I2C )


Wire.send(bin2bcd(dt.year() - 2000));
Wire.send(i);
Wire.endTransmission();
}
DateTime RTC_DS1307::now() {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.send(i);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
uint8_t ss = bcd2bin(Wire.receive() & 0x7F);
uint8_t mm = bcd2bin(Wire.receive());
uint8_t hh = bcd2bin(Wire.receive());
Wire.receive();
uint8_t d = bcd2bin(Wire.receive());
uint8_t m = bcd2bin(Wire.receive());
uint16_t y = bcd2bin(Wire.receive()) + 2000;
return DateTime (y, m, d, hh, mm, ss);
}
#endif
// RTC_Millis implementation
long RTC_Millis::offset = 0;
void RTC_Millis::adjust(const DateTime& dt) {
offset = dt.unixtime() - millis() / 1000;
}
DateTime RTC_Millis::now() {
return (uint32_t)(offset + millis() / 1000);
}

RTClib.h (Tiny RTC I2C include )


// Simple general-purpose date/time class (no TZ / DST / leap second
handling!)
class DateTime {
public:
~ 200 ~

RTClib.h (Tiny RTC I2C include )


DateTime (uint32_t t =0);
DateTime (uint16_t year, uint8_t month, uint8_t day,
uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
DateTime (const char* date, const char* time);
uint16_t year() const
{ return 2000 + yOff; }
uint8_t month() const
{ return m; }
uint8_t day() const
{ return d; }
uint8_t hour() const
{ return hh; }
uint8_t minute() const
{ return mm; }
uint8_t second() const
{ return ss; }
uint8_t dayOfWeek() const;
// 32-bit times as seconds since 1/1/2000
long secondstime() const;
// 32-bit times as seconds since 1/1/1970
uint32_t unixtime(void) const;
protected:
uint8_t yOff, m, d, hh, mm, ss;
};
// RTC based on the DS1307 chip connected via I2C and the Wire library
class RTC_DS1307 {
public:
static uint8_t begin(void);
static void adjust(const DateTime& dt);
uint8_t isrunning(void);
static DateTime now();
};
// RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
class RTC_Millis {
public:
static void begin(const DateTime& dt) { adjust(dt); }
static void adjust(const DateTime& dt);
static DateTime now();
protected:
static long offset;
};
~ 201 ~

HX711

(, 2013)

~ 202 ~

~ 203 ~

~ 204 ~

~ 205 ~

~ 206 ~

~ 207 ~

~ 208 ~

~ 209 ~

~ 210 ~

LCDKeypad Shield

(DFRobot, 2013)

~ 211 ~

LCM 1602

(DFRobot, 2013, Guangzhou_Tinsharp_Industrial_Corp._Ltd., 2013)

~ 212 ~

~ 213 ~

~ 214 ~

~ 215 ~

~ 216 ~

~ 217 ~

~ 218 ~

~ 219 ~

~ 220 ~

~ 221 ~

~ 222 ~

~ 223 ~

~ 224 ~

~ 225 ~

~ 226 ~

~ 227 ~


Adafruit Industries LiquidCrystal library for arduino. (2013,
2013.7.3).https://github.com/adafruit/LiquidCrystal
Arduino Arduino official website. (2013, 2013.7.3).http://www.arduino.cc/
Atmel Corporation Atmel Corporation Website. (2013,
2013.6.17).http://www.atmel.com/
Banzi, M., Getting Started with arduino: Make, 2009.
Beckwith, T. G., Marangoni, R. D., and Lienhard, J. H., Mechanical measurements:
Pearson Prentice Hall, 2007.
C, R. A., "Strain responsive apparatus," U.S.A. Patent, 1943.
Coover, H. W., "Stabilized

Alpha-Cyanoacrylate," 1956.

Creative Commons Creative Commons. (2013, 2013.7.3).


http://en.wikipedia.org/wiki/Creative_Commons
Crowe, C., Gulick, D. E., and Lawell, T. G., "Universal asynchronous
receiver-transmitter," 1988.
dea314@gmail.com hx711 library for arduino platform (2013, 2013.6.27).
http://code.google.com/p/hx711-arduino/downloads/list
DFRobot Arduino LCD KeyPad Shield (2013,
2013.7.3).http://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield
_(SKU:_DFR0009)
Guangzhou Tinsharp Industrial Co., Ltd. TC1602A DataSheet. (2013,
2013.7.7).http://www.tinsharp.com/
Gulick, D. E., Lawell, T. G., and Crowe, C., "Enhanced universal asynchronous
receiver-transmitter," 1990.
~ 228 ~

Gulick, D. E., Lawell, T. G., and Crowe, C., "Universal asynchronous


receiver-transmitter," 1993.
Hannah, R. L. and Reed, S. E., Strain gage users' handbook: Springer, 1992.
Interaction Design Lab Fritzing (2013, 2013.7.22).http://fritzing.org/
Adafruit Industries A fork of Jeelab's fantastic RTC library. (2013,
2013.7.10).https://github.com/adafruit/RTClib
Michael, M. S., "Universal asynchronous receiver/transmitter," 1989.
Michael, M. S., "Universal asynchronous receiver/transmitter," 1993.
Michael, M. S., "Universal asynchronous receiver/transmitter," 1995.
Michael, M. S., "Universal asynchronous receiver/transmitter," 1996.
Michael, M. S., "Universal asynchronous receiver/transmitter," 1999.
Ben Fry and Casey Reas Processing. (2013,
2013.6.17).http://www.processing.org/
Reas, C. and Fry, B., Processing: a programming handbook for visual designers
and artists vol. 6812: Mit Press, 2007.
Reas, C. and Fry, B., Getting Started with Processing: Make, 2010.
Ruge, A. C., "Gauge," U.S.A. Patent, 1943.
Shoberg, R. S., "Force Transducer Loda Cell," U.S.A. Patent, 1971.
Shoberg, R. S., "Load cell," U.S.A. Patent, 1976.
Shoberg, R. S., "Load cell," U.S.A. Patent, 1978.
Simmons, E. E., "Material Testing Apparatus," U.S.A. Patent, 1942.

~ 229 ~

Simmons, E. E., "Strain Gauge and Method for Making Same," U.S.A. Patent,
1944.
Stein, P. K., "A brief history of bonded resistance strain gages from conception to
commercialization," Experimental Techniques, vol. 14, pp. 13-19, 1990.
Story, F. H., Harrow, S. E., and Simmons, L. E., "Programming interface for a
universal asynchronous receiver/transmitter," 1998.
Takahashi, Y. and Ibuka, T., "Micro-processor unit having universal asynchronous
receiver/transmitter," 1998.
Simon Tatham PuTTY Download Page. (2013,
2013.7.8).http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Window, A. and Holister, G. S., Strain gauge technology: Applied science
publishers, 1982.
() . (2013). .
2013.http://www.aviaic.com/
, , , and , "," ,
, , ,, 2003.

~ 230 ~