Elctronic Brick :Analog and Digital
Analog signals are continuous where digital signals are discrete. Analog signals are continuously varying where digital signals are based on 0’s and 1’s (or as often said——- on’s and off’s). As an analogy, consider a light switch that is either on or off (digital) and a dimmer switch (analog) that allows you to vary the light in different degrees of brightness.
Now we talk about the Digital signal first. The button or the digital sensor can be a digital input to Arduino. Arduino can read these digital signals by the digital Pin (almost all the pin is suppose digital input).And also can write the digital signal out by these digital Pins.
Here is an example for the beginner to understand the digital input and output. We use a button to control the LED on or off.
What we need is an Arduino, an Electronic Brick chassis, a button brick and, LED brick and two signal cables. Hook up the button Brick to the D9 connector of chassis, then the button is connects with the 9th digital pin of Arduino. And hook up the LED Brick to the D8 connector of chassis, then the LED is connects with the 8th digital pin of Arduino.
The hardware is done, and now we can open the ArduinoIDE to write the software .
The code is below:
int Button = 9; //define the 9th digital pin for button brick int LED = 8; //define the 8th digital pin for LED brick
void setup() { pinMode(LED,OUTPUT); //set the LED pin for digital output pinMode(Button,INPUT); //set the Button pin for digital input }
void loop() { if (digitalRead(Button)) // if button press digitalWrite(LED,HIGH); // light the LED else // if not press digitalWrite(LED,LOW); // turn off the LED }
Program the code to Arduino .And then if you press the button the LED will light and if you release the button the LED turn off.
The Arduino can Read the Analog signal via the AD by the analog pin, and put out the analog signal by PWM pins. In Arduino there are 6 analog input pin from A0-A1, and there are 6 analog output pins: D3 D5 D6 D9 D10 D11.
We use a buzzer brick and a Rotary brick to show the analog input and output .Hook up the buzzer to D9 connector of chassis and hook up the rotary brick to A1 connector of chassis.
And the code below:
int Rotary = 1; // define the Rotary for 1th Analog pin int Buzzer = 9; // define the Buzzer for 9th Digital pin- //which is Analog out pin also int val = 0;
void setup() { }
void loop() { val = analogRead(Rotary); // read the Rotary analog value analogWrite(Buzzer,val); // Write the analog value out to Buzzer }
Program the code into Arduino, and when you rotate the rotary brick, the buzzer sound will change.