Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
124 views

Arduino Uno: Operating System CPU

The Arduino Uno is an open-source microcontroller board based on the ATmega328P microcontroller. It has 14 digital input/output pins, 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. The Arduino Uno board was the first in the Arduino platform and is still widely used due to its low cost and ease of use. It defines the shield standard and programming environment for the Arduino platform of microcontroller boards.

Uploaded by

Jemz Villaret
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Arduino Uno: Operating System CPU

The Arduino Uno is an open-source microcontroller board based on the ATmega328P microcontroller. It has 14 digital input/output pins, 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. The Arduino Uno board was the first in the Arduino platform and is still widely used due to its low cost and ease of use. It defines the shield standard and programming environment for the Arduino platform of microcontroller boards.

Uploaded by

Jemz Villaret
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Arduino Uno

From Wikipedia, the free encyclopedia

Jump to navigation Jump to search

Arduino UNO

Arduino Uno SMD R3

Developer Arduino

Manufacturer Many

Type Single-board microcontroller[1]

Operating system None

CPU Microchip AVR (8-bit)

Memory SRAM

Storage Flash, EEPROM

The Arduino Uno is an open-source microcontroller board based on the Microchip ATmega328P
microcontroller and developed by Arduino.cc. The board is equipped with sets of digital and analog
input/output (I/O) pins that may be interfaced to various expansion boards (shields) and other circuits.
The board has 14 Digital pins, 6 Analog pins, and programmable with the Arduino IDE (Integrated
Development Environment) via a type B USB cable. It can be powered by the USB cable or by an external
9-volt battery, though it accepts voltages between 7 and 20 volts. It is also similar to the Arduino Nano
and Leonardo. The hardware reference design is distributed under a Creative Commons Attribution
Share-Alike 2.5 license and is available on the Arduino website. Layout and production files for some
versions of the hardware are also available.
The word "uno" means "one" in Italian and was chosen to mark the initial release of the Arduino
Software. The Uno board is the first in a series of USB-based Arduino boards, and it and version 1.0 of
the Arduino IDE were the reference versions of Arduino, now evolved to newer releases. The
ATmega328 on the board comes preprogrammed with a bootloader that allows uploading new code to
it without the use of an external hardware programmer.

While the Uno communicates using the original STK500 protocol, it differs from all preceding boards in
that it does not use the FTDI USB-to-serial driver chip. Instead, it uses the Atmega16U2 (Atmega8U2 up
to version R2) programmed as a USB-to-serial converter.[7]

Contents

 1 Background

 2 Technical specifications

 3 Pins

o 3.1 General pin functions

o 3.2 Special pin functions

 4 Communication

o 4.1 Automatic (software) reset

 5 See also

 6 References

 7 External links

Background

an early production board

The Arduino project started at the Interaction Design Institute Ivrea (IDII) in Ivrea, Italy. At that time, the
students used a BASIC Stamp microcontroller at a cost of $100, a considerable expense for many
students. In 2003 Hernando Barragán created the development platform Wiring as a Master's thesis
project at IDII, under the supervision of Massimo Banzi and Casey Reas, who are known for work on the
Processing language. The project goal was to create simple, low-cost tools for creating digital projects by
non-engineers. The Wiring platform consisted of a printed circuit board (PCB) with an ATmega168
microcontroller, an IDE based on Processing and library functions to easily program the microcontroller.
In 2003, Massimo Banzi, with David Mellis, another IDII student, and David Cuartielles, added support
for the cheaper ATmega8 microcontroller to Wiring. But instead of continuing the work on Wiring, they
forked the project and renamed it Arduino. Early arduino boards used the FTDI USB-to-serial driver chip
and an ATmega168. The Uno differed from all preceding boards by featuring the ATmega328P
microcontroller and an ATmega16U2 (Atmega8U2 up to version R2) programmed as a USB-to-serial
converter.

Technical specifications

 Microcontroller: Microchip ATmega328P [7]

 Operating Voltage: 5 Volts

 Input Voltage: 7 to 20 Volts

 Digital I/O Pins: 14 (of which 6 provide PWM output)

 Analog Input Pins: 6

 DC Current per I/O Pin: 20 mA

 DC Current for 3.3V Pin: 50 mA

 Flash Memory: 32 KB of which 0.5 KB used by bootloader

 SRAM: 2 KB

 EEPROM: 1 KB

 Clock Speed: 16 MHz

 Length: 68.6 mm

 Width: 53.4 mm

 Weight: 25 g
Pins

Arduino UNO

General pin functions

 LED: There is a built-in LED driven by digital pin 13. When the pin is high value, the LED is on,
when the pin is low, it's off.

 VIN: The input voltage to the Arduino/Genuino board when it's using an external power source
(as opposed to 5 volts from the USB connection or other regulated power source). You can
supply voltage through this pin, or, if supplying voltage via the power jack, access it through this
pin.

 5V: This pin outputs a regulated 5V from the regulator on the board. The board can be supplied
with power either from the DC power jack (7 - 20V), the USB connector (5V), or the VIN pin of
the board (7-20V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can
damage the board.

 3V3: A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.

 GND: Ground pins.

 IOREF: This pin on the Arduino/Genuino board provides the voltage reference with which the
microcontroller operates. A properly configured shield can read the IOREF pin voltage and select
the appropriate power source or enable voltage translators on the outputs to work with the 5V
or 3.3V.

 Reset: Typically used to add a reset button to shields which block the one on the board.

Special pin functions

Each of the 14 digital pins and 6 analog pins on the Uno can be used as an input or output, using
pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or
receive 20 mA as recommended operating condition and has an internal pull-up resistor (disconnected
by default) of 20-50k ohm. A maximum of 40mA is the value that must not be exceeded on any I/O pin
to avoid permanent damage to the microcontroller. The Uno has 6 analog inputs, labeled A0 through A5,
each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from
ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and
the analogReference() function.
In addition, some pins have specialized functions:

 Serial / UART: pins 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data.
These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL serial chip.

 External interrupts: pins 2 and 3. These pins can be configured to trigger an interrupt on a low
value, a rising or falling edge, or a change in value.

 PWM (pulse-width modulation): 3, 5, 6, 9, 10, and 11. Can provide 8-bit PWM output with the
analogWrite() function.

 SPI (Serial Peripheral Interface): 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI
communication using the SPI library.

 TWI (two-wire interface) / I²C: A4 or SDA pin and A5 or SCL pin. Support TWI communication
using the Wire library.

 AREF (analog reference): Reference voltage for the analog inputs.

Communication

The Arduino/Genuino Uno has a number of facilities for communicating with a computer, another
Arduino/Genuino board, or other microcontrollers. The ATmega328 provides UART TTL (5V) serial
communication, which is available on digital pins 0 (RX) and 1 (TX). An ATmega16U2 on the board
channels this serial communication over USB and appears as a virtual com port to software on the
computer. The 16U2 firmware uses the standard USB COM drivers, and no external driver is needed.
However, on Windows, a .inf file is required. The Arduino Software (IDE) includes a serial monitor which
allows simple textual data to be sent to and from the board. The RX and TX LEDs on the board will flash
when data is being transmitted via the USB-to-serial chip and USB connection to the computer (but not
for serial communication on pins 0 and 1). A SoftwareSerial library allows serial communication on any
of the Uno's digital pins.

Automatic (software) reset

Rather than requiring a physical press of the reset button before an upload, the Arduino/Genuino Uno
board is designed in a way that allows it to be reset by software running on a connected computer. One
of the hardware flow control lines (DTR) of the ATmega8U2/16U2 is connected to the reset line of the
ATmega328 via a 100 nanofarad capacitor. When this line is asserted (taken low), the reset line drops
long enough to reset the chip.

This setup has other implications. When the Uno is connected to a computer running Mac OS X or Linux,
it resets each time a connection is made to it from software (via USB). For the following half-second or
so, the bootloader is running on the Uno. While it is programmed to ignore malformed data (i.e.
anything besides an upload of new code), it will intercept the first few bytes of data sent to the board
after a connection is opened.
Arduino DC Motor Control Tutorial – L298N
| PWM | H-Bridge
Dejan

Arduino Tutorials

53

In this Arduino Tutorial we will learn how to control DC motors using Arduino. We well take a
look at some basic techniques for controlling DC motors and make two example through which
we will learn how to control DC motors using the L298N driver and the Arduino board.

Overview

We can control the speed of the DC motor by simply controlling the input voltage to the motor
and the most common method of doing that is by using PWM signal.

PWM DC Motor Control


PWM, or pulse width modulation is a technique which allows us to adjust the average value of
the voltage that’s going to the electronic device by turning on and off the power at a fast rate.
The average voltage depends on the duty cycle, or the amount of time the signal is ON versus the
amount of time the signal is OFF in a single period of time.
So depending on the size of the motor, we can simply connect an Arduino PWM output to the
base of transistor or the gate of a MOSFET and control the speed of the motor by controlling the
PWM output. The low power Arduino PWM signal switches on and off the gate at the MOSFET
through which the high power motor is driven.

Note: Arduino GND and the motor power supply GND should be connected together.
H-Bridge DC Motor Control
On the other hand, for controlling the rotation direction, we just need to inverse the direction of
the current flow through the motor, and the most common method of doing that is by using an H-
Bridge. An H-Bridge circuit contains four switching elements, transistors or MOSFETs, with the
motor at the center forming an H-like configuration. By activating two particular switches at the
same time we can change the direction of the current flow, thus change the rotation direction of
the motor.

So if we combine these two methods, the PWM and the H-Bridge, we can have a complete
control over the DC motor. There are many DC motor drivers that have these features and the
L298N is one of them.

L298N Driver
The L298N is a dual H-Bridge motor driver which allows speed and direction control of two DC
motors at the same time. The module can drive DC motors that have voltages between 5 and
35V, with a peak current up to 2A.
Let’s take a closer look at the pinout of L298N module and explain how it works. The module
has two screw terminal blocks for the motor A and B, and another screw terminal block for the
Ground pin, the VCC for motor and a 5V pin which can either be an input or output.

This depends on the voltage used at the motors VCC. The module have an onboard 5V regulator
which is either enabled or disabled using a jumper. If the motor supply voltage is up to 12V we
can enable the 5V regulator and the 5V pin can be used as output, for example for powering our
Arduino board. But if the motor voltage is greater than 12V we must disconnect the jumper
because those voltages will cause damage to the onboard 5V regulator. In this case the 5V pin
will be used as input as we need connect it to a 5V power supply in order the IC to work
properly.
We can note here that this IC makes a voltage drop of about 2V. So for example, if we use a 12V
power supply, the voltage at motors terminals will be about 10V, which means that we won’t be
able to get the maximum speed out of our 12V DC motor.

Next are the logic control inputs. The Enable A and Enable B pins are used for enabling and
controlling the speed of the motor. If a jumper is present on this pin, the motor will be enabled
and work at maximum speed, and if we remove the jumper we can connect a PWM input to this
pin and in that way control the speed of the motor. If we connect this pin to a Ground the motor
will be disabled.

Next, the Input 1 and Input 2 pins are used for controlling the rotation direction of the motor A,
and the inputs 3 and 4 for the motor B. Using these pins we actually control the switches of the
H-Bridge inside the L298N IC. If input 1 is LOW and input 2 is HIGH the motor will move
forward, and vice versa, if input 1 is HIGH and input 2 is LOW the motor will move backward.
In case both inputs are same, either LOW or HIGH the motor will stop. The same applies for the
inputs 3 and 4 and the motor B.
Arduino and L298N
Now let’s make some practical applications. In the first example we will control the speed of the
motor using a potentiometer and change the rotation direction using a push button. Here’s the
circuit schematics.

So we need an L298N driver, a DC motor, a potentiometer, a push button and an Arduino board.

You can get the components needed for this Arduino Tutorial from the links below:

 L298N Driver ……………………………….. Amazon / Banggood


 12V High Torque DC Motor ………….. Amazon / Banggood
 DC Motor w/ Plastic Tire Wheel ……. Amazon / Banggood
 Arduino Board ……………………………… Amazon / Banggood
 Breadboard and Jump Wires ………… Amazon / Banggood

*Please note: These are affiliate links. I may make a commission if you buy the components
through these links. I would appreciate your support in this way!

Arduino Code
Here’s the Arduino code:

1. /* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01


2.
3. by Dejan Nedelkovski, www.HowToMechatronics.com
4. */
5.
6. #define enA 9
7. #define in1 6
8. #define in2 7
9. #define button 4
10.
11. int rotDirection = 0;
12. int pressed = false;
13.
14. void setup() {
15. pinMode(enA, OUTPUT);
16. pinMode(in1, OUTPUT);
17. pinMode(in2, OUTPUT);
18. pinMode(button, INPUT);
19. // Set initial rotation direction
20. digitalWrite(in1, LOW);
21. digitalWrite(in2, HIGH);
22. }
23.
24. void loop() {
25. int potValue = analogRead(A0); // Read potentiometer value
26. int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to
255
27. analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
28.
29. // Read button - Debounce
30. if (digitalRead(button) == true) {
31. pressed = !pressed;
32. }
33. while (digitalRead(button) == true);
34. delay(20);
35.
36. // If button is pressed - change rotation direction
37. if (pressed == true & rotDirection == 0) {
38. digitalWrite(in1, HIGH);
39. digitalWrite(in2, LOW);
40. rotDirection = 1;
41. delay(20);
42. }
43. // If button is pressed - change rotation direction
44. if (pressed == false & rotDirection == 1) {
45. digitalWrite(in1, LOW);
46. digitalWrite(in2, HIGH);
47. rotDirection = 0;
48. delay(20);
49. }
50. }

Description: So first we need to define the pins and some variables needed for the program. In
the setup section we need to set the pin modes and the initial rotation direction of the motor. In
the loop section we start by reading the potentiometer value and then map the value that we get
from it which is from 0 to 1023, to a value from 0 to 255 for the PWM signal, or that’s 0 to
100% duty cycle of the PWM signal. Then using the analogWrite() function we send the PWM
signal to the Enable pin of the L298N board, which actually drives the motor.

Next, we check whether we have pressed the button, and if that’s true, we will change the
rotation direction of the motor by setting the Input 1 and Input 2 states inversely. The push
button will work as toggle button and each time we press it, it will change the rotation direction
of the motor.

Arduino Robot Car Control using L298N


Driver
So once we have learned this, now we can build our own Arduino robot car. Here’s the circuit
schematic:

All we need is 2 DC Motors, the L298N driver, an Arduino board and a joystick for the control.
As for the power supply, I chose to use three 3.7V Li-ion batteries, providing total of 11V. I
made the chassis out of 3 mm tick plywood, attached the motors to it using metal brackets,
attached wheels to the motors and in front attached a swivel wheel.

Now let’s take a look at the Arduino code and see how it works. (Down below you can find the
complete code)

1. int xAxis = analogRead(A0); // Read Joysticks X-axis


2. int yAxis = analogRead(A1); // Read Joysticks Y-axis
After defining the pins, in the loop section, we start with reading the joystick X and Y axis
values. The joystick is actually made of two potentiometers which are connected to the analog
inputs of the Arduino and they have values from 0 to 1023. When the joystick stays in its center
position the value of both potentiometers, or axes is around 512.

We will add a little tolerance and consider the values from 470 to 550 as center. So if we move
the Y axis of joystick backward and the value goes below 470 we will set the two motors rotation
direction to backward using the four input pins. Then, we will convert the declining values from
470 to 0 into increasing PWM values from 0 to 255 which is actually the speed of the motor.

1. // Y-axis used for forward and backward control


2. if (yAxis < 470) {
3. // Set Motor A backward
4. digitalWrite(in1, HIGH);
5. digitalWrite(in2, LOW);
6. // Set Motor B backward
7. digitalWrite(in3, HIGH);
8. digitalWrite(in4, LOW);
9. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for
the PWM signal for increasing the motor speed
10. motorSpeedA = map(yAxis, 470, 0, 0, 255);
11. motorSpeedB = map(yAxis, 470, 0, 0, 255);
12. }

Similar, if we move the Y axis of the joystick forward and the value goes above 550 we will set
the motors to move forward and convert the readings from 550 to 1023 into PWM values from 0
to 255. If the joystick stays in its center the motors speed will be zero.

Next, let’s see how we use the X axis for the left and right control of the car.

1. // X-axis used for left and right control


2. if (xAxis < 470) {
3. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
4. int xMapped = map(xAxis, 470, 0, 0, 255);
5. // Move to left - decrease left motor speed, increase right motor speed
6. motorSpeedA = motorSpeedA - xMapped;
7. motorSpeedB = motorSpeedB + xMapped;
8. // Confine the range from 0 to 255
9. if (motorSpeedA < 0) {
10. motorSpeedA = 0;
11. }
12. if (motorSpeedB > 255) {
13. motorSpeedB = 255;
14. }
15. }

So again, first we need to convert the X axis readings into speed values from 0 to 255. For
moving left, we use this value to decrease the left motor speed and increase the right motor
speed. Here, because of the arithmetic functions we use two additional “if” statements to confine
the range of the motor speed from 0 to 255.

The same method is used for moving the car to the right.

Depending on the applied voltage and the motor itself, at lower speeds the motor is not able to
start moving and it produces a buzzing sound. In my case, the motors were not able to move if
the value of the PWM signal was below 70. Therefore using this two if statements I actually
confined to speed range from 70 to 255. At the end we just send the final motor speeds or PWM
signal to the enable pins of the L298N driver.

1. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start
moving if PWM value was below value of 70)
2. if (motorSpeedA < 70) {
3. motorSpeedA = 0;
4. }
5. if (motorSpeedB < 70) {
6. motorSpeedB = 0;
7. }
8. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
9. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

Here’s the complete code of the Arduino robot car example:

1. /* Arduino DC Motor Control - PWM | H-Bridge | L298N


2. Example 02 - Arduino Robot Car Control
3. by Dejan Nedelkovski, www.HowToMechatronics.com
4. */
5.
6. #define enA 9
7. #define in1 4
8. #define in2 5
9. #define enB 10
10. #define in3 6
11. #define in4 7
12.
13. int motorSpeedA = 0;
14. int motorSpeedB = 0;
15.
16. void setup() {
17. pinMode(enA, OUTPUT);
18. pinMode(enB, OUTPUT);
19. pinMode(in1, OUTPUT);
20. pinMode(in2, OUTPUT);
21. pinMode(in3, OUTPUT);
22. pinMode(in4, OUTPUT);
23. }
24.
25. void loop() {
26. int xAxis = analogRead(A0); // Read Joysticks X-axis
27. int yAxis = analogRead(A1); // Read Joysticks Y-axis
28.
29. // Y-axis used for forward and backward control
30. if (yAxis < 470) {
31. // Set Motor A backward
32. digitalWrite(in1, HIGH);
33. digitalWrite(in2, LOW);
34. // Set Motor B backward
35. digitalWrite(in3, HIGH);
36. digitalWrite(in4, LOW);
37. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for
the PWM signal for increasing the motor speed
38. motorSpeedA = map(yAxis, 470, 0, 0, 255);
39. motorSpeedB = map(yAxis, 470, 0, 0, 255);
40. }
41. else if (yAxis > 550) {
42. // Set Motor A forward
43. digitalWrite(in1, LOW);
44. digitalWrite(in2, HIGH);
45. // Set Motor B forward
46. digitalWrite(in3, LOW);
47. digitalWrite(in4, HIGH);
48. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value
for the PWM signal for increasing the motor speed
49. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
50. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
51. }
52. // If joystick stays in middle the motors are not moving
53. else {
54. motorSpeedA = 0;
55. motorSpeedB = 0;
56. }
57.
58. // X-axis used for left and right control
59. if (xAxis < 470) {
60. // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
61. int xMapped = map(xAxis, 470, 0, 0, 255);
62. // Move to left - decrease left motor speed, increase right motor speed
63. motorSpeedA = motorSpeedA - xMapped;
64. motorSpeedB = motorSpeedB + xMapped;
65. // Confine the range from 0 to 255
66. if (motorSpeedA < 0) {
67. motorSpeedA = 0;
68. }
69. if (motorSpeedB > 255) {
70. motorSpeedB = 255;
71. }
72. }
73. if (xAxis > 550) {
74. // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
75. int xMapped = map(xAxis, 550, 1023, 0, 255);
76. // Move right - decrease right motor speed, increase left motor speed
77. motorSpeedA = motorSpeedA + xMapped;
78. motorSpeedB = motorSpeedB - xMapped;
79. // Confine the range from 0 to 255
80. if (motorSpeedA > 255) {
81. motorSpeedA = 255;
82. }
83. if (motorSpeedB < 0) {
84. motorSpeedB = 0;
85. }
86. }
87. // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start
moving if PWM value was below value of 70)
88. if (motorSpeedA < 70) {
89. motorSpeedA = 0;
90. }
91. if (motorSpeedB < 70) {
92. motorSpeedB = 0;
93. }
94. analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
95. analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
96. }

So that would be all for this tutorial, and in my next video we will upgrade this Arduino robot
car, by adding a Bluetooth and Radio devices for enabling smartphone and wireless control.

double
[Data Types]

Description

Double precision floating point number. On the Uno and other ATMEGA based boards, this
occupies 4 bytes. That is, the double implementation is exactly the same as the float, with no
gain in precision.

On the Arduino Due, doubles have 8-byte (64 bit) precision.

Syntax

double var = val;

Parameters

var: variable name.


val: the value to assign to that variable.

Notes and Warnings

Users who borrow code from other sources that includes double variables may wish to examine
the code to see if the implied precision is different from that actually achieved on ATMEGA
based Arduinos.
In Java, floating point numbers are numbers that have a decimal part to them. Java has two basic
floating point data types - float and double. In this lesson, we'll learn what they are and how to
use them.

Floating Point Numbers in Java


Floating point numbers are also called floating point literals in Java. For calculations using
decimal values, Java supports two basic data types: float and double. Together, these support
decimal or floating point numbers in the Java programming language. Floating point numbers are
also known as real numbers and are used when we need precision in calculations. For example,
floating point or decimal numbers are used when expressing currency (dollars and cents),
measurements (feet and inches), and time (hours, minutes, and seconds). Decimal values are
useful because they allow more accuracy and precision.

A float data type is a single precision number format that occupies 4 bytes or 32 bits in
computer memory. As we already know, a bit is a single unit that stores a single value 0 or 1.
Bits are grouped in groups of 8 to make 1 byte.

A double data type is a double precision number format that occupies 8 bytes or 64 bits in the
memory of a computer. This is twice the number of bytes occupied by floats, which is single
precision.

Both float and double data types can store positive and negative values.

Float & Double Data Type


A float data type in Java stores a decimal value with 6-7 total digits of precision. So, for
example, 12.12345 can be saved as a float, but 12.123456789 can't be saved as a float. When
representing a float data type in Java, we should append the letter f to the end of the data type;
otherwise it will save as double.

The default value of a float in Java is 0.0f. Float data type is used when you want to save
memory and when calculations don't require more than 6 or 7 digits of precision.

Let's take a look at a few examples:

1. float a = 2.356f;

Here the variable a saves the value 2.356 as a float.

1. float x = -125.563f;

Here the variable x saves the value -125.563 as a float.

1. float q = 506.12789f;
Here the variable q saves the value 506.1247 as a float.

1. float r = -101.23;

Here the variable r isn't stored as a float; instead it's stored as a double because the value 101.23
isn't appended with the letter f.

1. float c = 101.123456789123f;

Here the variable c is attempting to store a float value with 12 digits after the decimal. However,
a float in Java can only store a maximum of 7 digits, so this will cause an error.

1. float p = 25f;

Here the variable p will store the value 25 as a float data type in Java.

The double data type stores decimal values with 15-16 digits of precision. Remember that the
default value is 0.0d, so this means that if you don't append f or d to the end of the decimal, the
value will be stored as a double in Java. Let's now consider a few examples:

1. double a = 2.356;

Here the variable a saves the value 2.356 as a double.

1. double x = -125.563

Here the variable x saves the value -125.563 as a double.

1. double q = 506.124789;

Here the variable q saves the value 506.124789 as a double.

1. double r = -101.1234567891234567;

Here the variable r saves the value 101.1234567891234567 as a double.

1. double c = 101.123456789123456789;

Here the variable c is attempting to store a double value with 18 digits after the decimal.
However, a double in Java can only store a maximum of 16 digits, so this will cause an error.

1. double p = 25;

Here the variable p will store the value 25 as a double data type in Java.
Caution with Using Double & Float
Double and float aren't used to represent values that require a very high precision. So, for
instance, to store currency values, it's not a good idea to use double or float; instead Java has a
class called Big Decimal that specifies the exact number of digits following the decimal,
including rounding up or down.

Consider the following example in Java:

1. double k = 3.5 + 4.9;

You would expect the result to be 8.4, correct? Instead, if you print the result out in Java, you
would see that k = 8.3999999999999999;

This is because double can store a maximum of 16 digits after the decimal, and this may be very
accurate, but it won't work for calculations involving dollars and cents.

Example with dollars and cents using double:

Suppose you want to find the sum of two purchases you made in a day, and you store the value
of the 2 purchases in double data format in Java, like so:

1. double purchase1 = 35.40;


2. double purchase2 = 42.90;

Now to find the sum you declare another double variable sum and compute the result like so:

1. double sum = purchase1 + purchase2;?

You might also like