Mechabau® Coding BlockTM (U1) Pinouts

Coding BlockTM U1
The Coding BlockTM module (U1) includes an Arduino Nano circuit board. This is a mini computer which can be programmed to perform different tasks, including monitoring things and making things happen.
Coding BlockTM does not have an on/off switch when powered using the USB cable. To turn it off, disconnect the USB cable from your computer.
U1 Coding BlockTM connections:
(+) – power
(−) – power
A0, A4, A5 – Analog inputs
D2, D3, D7, D8, D9 – Digital inputs/outputs
You can also use the Coding BlockTM for your other projects.
The USB cable is used to program and communicate with the Coding BlockTM (U1).
Notes for using the Coding BlockTM module in other applications:
Power source: Coding BlockTM should only be powered using a USB cable and the 9V battery holder included in the set. Coding BlockTM should never be used with Block Circuits® battery holders from other sets or other power sources.
Analog inputs: These can measure voltage with 10-bit accuracy (1024 levels). They can also be configured to act as additional digital inputs/outputs.
Digital inputs/outputs: When configured as inputs the voltages should be above 80% of the power source voltage to be high, or below 20% of the power source voltage to be low. When configured as outputs each can supply or receive up to 20 mA; this is enough to light an LED, but an interface transistor may be needed when controlling a motor or speaker. These (D3 and D9) can be configured to simulate analog outputs using Pulse Width Modulation (PWM).
Project 1 Circuit (Blue Light)

Project 1 Explanation (Blue Light)
Block Circuits® uses electronic blocks that connect together to build different circuits. These blocks have different colors and numbers on them so you can easily identify them.
Build the circuit shown by placing all the parts. Install the code below to your Coding BlockTM and the blue LED (L2) lights.
Project 1 Code (Blue Light)
#define LED 9 // The pin the LED is connected to void setup() { pinMode(LED, OUTPUT); // Declare the LED as an output } void loop() { digitalWrite(LED, HIGH); // Turn the LED on }
Project 2 Circuit (Adding External Power)

Project 2 Explanation (Adding External Power)
Build the same circuit at project 1. After installing the program you do not need usb cable to powering your Coding BlockTM. Reconnect the usb cable and turn the switch on. Now use your 9V battery and connect it as shown on the circuit. Install a 9V battery into the 9V battery holder, plug it into the connector on the Coding BlockTM module (U1), and turn on the switch on the switch block (S1). Alternately you may power the circuit using the USB cable instead of the 9V battery.
Slide the switch header (S1), and the blue LED (L2) lights.
Project 2 Code (Adding External Power)
#define LED 9 // The pin the LED is connected to void setup() { pinMode(LED, OUTPUT); // Declare the LED as an output } void loop() { digitalWrite(LED, HIGH); // Turn the LED on }
Project 3 Circuit (Blinking Light)

Project 3 Explanation (Blinking Light)
This project explains the procedure for programming the Coding BlockTM module (U1). The microcontroller can be re-programmed in any circuit that uses it, by attaching the programming cable to it. When you initiate a new program download, any program currently running in the microcontroller is interrupted. When a new program download is complete, the new program will begin running.
The USB cable is needed to download new programs to the microcontroller, and to allow some programs to transfer information to/from the computer’s display. The USB also provides power to your circuits, so the 9V battery connector is ignored while you are connected to a USB device. Coding BlockTM has been programmed, you may disconnect the USB cable and run the circuit using the 9V battery connector.
Project 3 Code (Blinking Light)
void setup() { pinMode(2, OUTPUT); // We define the digital pin number 2 as an output to give electricity. } void loop() { digitalWrite(2, HIGH); // We provide electrical output from pin 2 with the HIGH command. delay(1000); // we are holding our code for 1000 milliseconds (1 second). digitalWrite(2, LOW); // We stop the electrical output with the LOW command from pin 2. delay(1000); // we are holding our code for 1000 milliseconds (1 second). }
Project 4 Circuit (All Lights On)

Project 4 Explanation (All Lights On)
Build the circuit, install the program, turn on the switch (S1). The white and blue LEDs (L1 and L2) will lights.
Project 4 Code (All Lights On)
#define LED 9 // The pin the LED is connected to void setup() { pinMode(LED, OUTPUT); // Declare the LED as an output } void loop() { digitalWrite(LED, HIGH); // Turn the LED on }
Project 5 Circuit (Switching and Flashing Lights)

Project 5 Explanation (Switching and Flashing Lights)
Build this circuit. Load sketch Switching and flashing Lights into Coding BlockTM using the programming instruction. Arduino controls the two circuits LEDs (white and blue) and alternates turning them on and off by flashing.
This sketch uses the int command (int is short for integer) to assign a constant value that will be used within the sketch. Not necessary to be used. You can change the blink rate by editing the delay value, then reloading it into Coding BlockTM. The microcontroller on the Arduino Nano board lets you control the LEDs in ways that would be difficult to do using switches or other devices.
Project 5 Code (Switching and Flashing Lights)
void setup() { pinMode(2, OUTPUT); // We define the digital pin number 2 as an output to give electricity. pinMode(3, OUTPUT); // We define the digital pin number 3 as an output to give electricity. } void loop() { //white LED flashes 1st time digitalWrite(2, HIGH); // We provide electrical output from pin 2 with the HIGH command. delay(50); // we hold our code for 50 milliseconds. digitalWrite(2, LOW); // We stop the electrical output with the LOW command from pin 2. delay(50); // we hold our code for 50 milliseconds. //red LED flashes 2nd time digitalWrite(2, HIGH); delay(50); digitalWrite(2, LOW); delay(50); //blue LED flashes 1st time digitalWrite(3, HIGH); delay(50); digitalWrite(3, LOW); delay(50); //blue LED flashes 2nd time digitalWrite(3, HIGH); delay(50); digitalWrite(3, LOW); delay(50); }
Project 6 Circuit (Flashing Lights and Buzzer)

Project 6 Explanation (Flashing Lights and Buzzer)
Build the project 5 and add buzzer module (BZ1) on the circuit as shown. Load sketch Flashing Lights and Buzzer into Coding BlockTM using the programming instruction. Arduino controls the three circuits LEDs (white and blue) and Buzzer. It alternates turning them on and off by flashing/beeping.
You can change the blink rate by editing the delay value, then reloading it into Coding BlockTM. The microcontroller on the Arduino Nano board lets you control the LEDs in ways that would be difficult to do using switches or other devices.
Project 6 Code (Flashing Lights and Buzzer)
void setup() { pinMode(2, OUTPUT); // We define the digital pin number 2 as an output to give electricity. pinMode(3, OUTPUT); // We define the digital pin number 3 as an output to give electricity. pinMode(7, OUTPUT); // We define the digital pin number 7 as an output to give electricity. } void loop() { //white LED flashes 1st time digitalWrite(2, HIGH); // We provide electrical output from pin 2 with the HIGH command. delay(50); // we hold our code for 50 milliseconds. digitalWrite(2, LOW); // We stop the electrical output with the LOW command from pin 2. delay(50); // we hold our code for 50 milliseconds. //red LED flashes 2nd time digitalWrite(2, HIGH); delay(50); digitalWrite(2, LOW); delay(50); //blue LED flashes 1st time digitalWrite(3, HIGH); delay(50); digitalWrite(3, LOW); delay(50); //blue LED flashes 2nd time digitalWrite(3, HIGH); delay(50); digitalWrite(3, LOW); delay(50); //Buzzer beeps 1 time digitalWrite(7, HIGH); delay(50); digitalWrite(7, LOW); delay(50); //Buzzer beeps 2 times digitalWrite(7, HIGH); delay(50); digitalWrite(7, LOW); delay(50); }
Project 7 Circuit (Lighting Led with Button)

Project 7 Explanation (Lighting Led with Button)
Build this circuit. Load sketch Switching and flashing Lights into Coding BlockTM using the programming instruction. Turn on the switch (S1) and LED (L2) should be on. Now Turn off the switch (S1) and LED (L2) should be off.
You have to use the PC in this project as your power source.
Not important if a program is installed or not on the Arduino Nano board because non of signal pins of the board is used.
Project 7 Code (Lighting Led with Button)
Project 8 Circuit (Reading Data from LDR)

Project 8 Explanation (Reading Data from LDR)
Build this circuit. When open the Serial Port screen we will see numbers flowing down the screen. By changing the amount of light into the photoresistor, the value of the number will also change as the voltage we send. The 330ohm resistor gives a constantly high value since the excess electrical voltage from the Photoresistor cannot be discharged over the cable. By throwing the excess voltage to GND (ground) we get more accurate value.
You have to use the PC in this project as your power source.
Project 8 Code (Reading Data from LDR)
void setup() { Serial.begin(9600); // We initialize the serial port and determine the communication speed (9600) between the arduino and the computer. } void loop() { // We use analogRead() command to read data from analog pins. Serial.println(analogRead(0)); // Serial.println() command prints the data we wrote in parentheses ( analogRead(0) ) to the Serial Port screen. }
Project 9 Circuit (Star Wars Theme Song)

Project 9 Explanation (Star Wars Theme Song)
In this project we are gonna see how to make the Star Wars theme song (imperial march) using arduino + buzzer and some leds (optional). By changing the frequency of the signal, you can get a musical note.
You have to use the PC in this project as your power source.
Project 9 Code (Star Wars Theme Song)
const int c = 261; const int d = 294; const int e = 329; const int f = 349; const int g = 391; const int gS = 415; const int a = 440; const int aS = 455; const int b = 466; const int cH = 523; const int cSH = 554; const int dH = 587; const int dSH = 622; const int eH = 659; const int fH = 698; const int fSH = 740; const int gH = 784; const int gSH = 830; const int aH = 880; const int buzzerPin = 8; // Digital Pin 8 const int ledPin1 = 12; // Digital Pin 12 const int ledPin2 = 13; // Digital Pin 13 Built In Led if you want int counter = 0; void setup() { //Setup pin modes pinMode(buzzerPin, OUTPUT); // Digital Pin 8 pinMode(ledPin1, OUTPUT); // Digital Pin 12 Built In Led it if you want pinMode(ledPin2, OUTPUT); // Digital Pin 13 Built In Led it if you want } void loop() { //Play first section firstSection(); //Play second section secondSection(); //Variant 1 beep(f, 250); beep(gS, 500); beep(f, 350); beep(a, 125); beep(cH, 500); beep(a, 375); beep(cH, 125); beep(eH, 650); delay(500); //Repeat second section secondSection(); //Variant 2 beep(f, 250); beep(gS, 500); beep(f, 375); beep(cH, 125); beep(a, 500); beep(f, 375); beep(cH, 125); beep(a, 650); delay(650); } void beep(int note, int duration) { //Play tone on buzzerPin tone(buzzerPin, note, duration); //Play different LED depending on value of 'counter' if(counter % 2 == 0) { digitalWrite(ledPin1, HIGH); delay(duration); digitalWrite(ledPin1, LOW); }else { digitalWrite(ledPin2, HIGH); delay(duration); digitalWrite(ledPin2, LOW); } //Stop tone on buzzerPin noTone(buzzerPin); delay(50); //Increment counter counter++; } void firstSection() { beep(a, 500); beep(a, 500); beep(a, 500); beep(f, 350); beep(cH, 150); beep(a, 500); beep(f, 350); beep(cH, 150); beep(a, 650); delay(500); beep(eH, 500); beep(eH, 500); beep(eH, 500); beep(fH, 350); beep(cH, 150); beep(gS, 500); beep(f, 350); beep(cH, 150); beep(a, 650); delay(500); } void secondSection() { beep(aH, 500); beep(a, 300); beep(a, 150); beep(aH, 500); beep(gSH, 325); beep(gH, 175); beep(fSH, 125); beep(fH, 125); beep(fSH, 250); delay(325); beep(aS, 250); beep(dSH, 500); beep(dH, 325); beep(cSH, 175); beep(cH, 125); beep(b, 125); beep(cH, 250); delay(350); }
Project 10 Circuit (Night Lamp)

Project 10 Explanation (Night Lamp)
Build this circuit. Load sketch Night Lamp into Coding Block using the programming instructions below. Cover the phototransistor (PR1) to turn on the red LED (L1). Once programmed, you can use the 9V battery connector to power the circuit instead of the USB cable, then take the circuit with you into a dark room. (Don’t forget, before moving the circuit replace it over a flat plate.)
You have to use the PC in this project as your power source.
You can adjust the sensitivity by changing the value 12 to be higher or lower.
Project 10 Code (Night Lamp)
int led = 3; int value; void setup(){ pinMode(led, OUTPUT); } void loop(){ value = analogRead(A0); if(value<12) // Adjust the sensitivity by changing the value digitalWrite(led, HIGH); else digitalWrite(led, LOW); }
Project 11 Circuit (Blink Rate)

Project 11 Explanation (Blink Rate)
Use the preceding circuit, but load sketch Blinking Light (Project 3) into Coding BlockTM. The red LED (L1) will be blinking at darkness. Insert code after value given line as shown below.
Project 11 Code (Blink Rate)
int led = 3; int value; void setup(){ pinMode(led, OUTPUT); } void loop(){ value = analogRead(A0); if(value<12) // Adjust the sensitivity by changing the value digitalWrite(3, HIGH); // We provide electrical output from pin 2 with the HIGH command. delay(1000); // we are holding our code for 1000 milliseconds (1 second). digitalWrite(3, LOW); // We stop the electrical output with the LOW command from pin 2. delay(1000); // we are holding our code for 1000 milliseconds (1 second). }
Project 12 Circuit (Night Siren)

Project 12 Explanation (Night Siren)
Build the circuit project 10. Now use buzzer instead a LED. Load sketch Night Siren into Coding Block using the programming instructions below. Cover the phototransistor (PR1) to turn on the Buzzer (BZ1). Once programmed, you can use the 9V battery connector to power the circuit instead of the USB cable, then take the circuit with you into a dark room. (Don’t forget, before moving the circuit replace it over a flat plate.)
You have to use the PC in this project as your power source.
You can adjust the sensitivity by changing the value 12 to be higher or lower.
Project 12 Code (Night Siren)
int buzzer = 3; int value; void setup(){ pinMode(buzzer, OUTPUT); } void loop(){ value = analogRead(A0); if(value<12) // Adjust the sensitivity by changing the value digitalWrite(buzzer, HIGH); else digitalWrite(buzzer, LOW); }
Project 13 Circuit (Flashing Siren at Night)

Project 13 Explanation (Flashing Siren at Night)
Use the preceding circuit, but load sketch Blinking Light (Project 3) into Coding BlockTM. The Buzzer (BZ1) will be blinking at darkness. Insert code after -value- given line as shown below.
Project 13 Code (Flashing Siren at Night)
int buzzer = 3; int value; void setup(){ pinMode(buzzer, OUTPUT); } void loop(){ value = analogRead(A0); if(value<12) // Adjust the sensitivity by changing the value digitalWrite(3, HIGH); // We provide electrical output from pin 2 with the HIGH command. delay(1000); // we are holding our code for 1000 milliseconds (1 second). digitalWrite(3, LOW); // We stop the electrical output with the LOW command from pin 2. delay(1000); // we are holding our code for 1000 milliseconds (1 second). }
Project 14 Circuit (Coding Block Test)

Project 14 Explanation (Coding Block Test)
This circuit tests the electrical connections on the Coding BlockTM module (U1), and is referenced by the Advanced Troubleshooting procedure on page A7 and A8. Build the circuit as shown, leaving one end of the red cable terminal (T) unconnected for now. Load sketch “Coding Block Test” into Coding BlockTM using the programming code below.
Connect the loose end of the red cable terminal (T) to each of the unused pins on the Coding BlockTM module (U1). One at a time; the blue LED (L2) should be blinking each time when touching it to the pins.
Remove the USB cable, then connect the 9V Battery (B3) and Switch (S1) Block and turn the switch ON; the circuit should work the same as with the USB cable.
Project 14 Code (Coding Block Test)
// Coding Block Test void setup(){ pinMode(A0, OUTPUT); // Define all connections. pinMode(A4, OUTPUT); pinMode(A5, OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); } void loop(){ digitalWrite(A0, HIGH); // We provide electrical output HIGH command. digitalWrite(A4, HIGH); digitalWrite(A5, HIGH); digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(7, HIGH); digitalWrite(8, HIGH); digitalWrite(9, HIGH); delay(250) ; digitalWrite(A0, LOW); // We provide electrical output LOW command. digitalWrite(A4, LOW); digitalWrite(A5, LOW); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(9, LOW); delay(250) ; }
Project 15 Circuit (Varying LED Brightness Gradually)

Project 15 Explanation (Varying LED Brightness Gradually)
Output using PWM broadens the way the LED lights up. Now, let’s change the PWM gradually to realize the effect that the LED brightens gradually.
Write a program as shown below.
The program stores the PWM ratio in the “i” variable used in the loop() function so that it can increment the value to make the LED gradually become brighter.
The while processing increases the value in increments specified by STEP until i reaches 255. The incremented value is output by analogWrite() to change the brightness of the LED. In addition, it is directed to wait for the duration specified by WAITTIME each time the PWM output is changed.
When the PWM ratio reaches 255, it is decreased until it reaches 0 to darken the LED gradually. You can modify the blinking speed by changing the value of WAITTIME or STEP. This time we’ve learned how to control the brightness of the LED.
Project 15 Code (Varying LED Brightness Gradually)
const int LED_PIN = 3; const int WAITTIME = 40; const int STEP = 10; void setup(){ pinMode( LED_PIN, OUTPUT ); } void loop(){ int i; i = 0; while ( i <= 255 ){ analogWrite( LED_PIN, i ); delay( WAITTIME ); i = i + STEP; } i = 255; while ( i >= 0 ){ analogWrite( LED_PIN, i ); delay( WAITTIME ); i = i - STEP; } }
Project 16 Circuit (Changing the Volume Gradually)

Project 16 Explanation (Changing the Volume Gradually)
Create project 15 and now replace the LED (L1) with Buzzer (BZ1). The same functionality applies to this project as well.
Project 16 Code (Changing the Volume Gradually)
const int Buzzer = 3; const int WAITTIME = 40; const int STEP = 10; void setup(){ pinMode( Buzzer, OUTPUT ); } void loop(){ int i; i = 0; while ( i <= 255 ){ analogWrite( Buzzer, i ); delay( WAITTIME ); i = i + STEP; } i = 255; while ( i >= 0 ){ analogWrite( Buzzer, i ); delay( WAITTIME ); i = i - STEP; } }
Project 17 Circuit (Changing the Light Intensity and Volume Gradually)

Project 17 Explanation (Changing the Light Intensity and Volume Gradually)
Use the preceding circuits and connect LED (L1) and Buzzer (BZ1) to pin D3. Connect the USB cable to Coding BlockTM and to PC as shown. Load the program to the Coding BlockTM and see what happens. Each block works together.
Project 17 Code (Changing the Light Intensity and Volume Gradually)
const int Pin = 3; const int WAITTIME = 40; const int STEP = 10; void setup(){ pinMode( Pin, OUTPUT ); } void loop(){ int i; i = 0; while ( i <= 255 ){ analogWrite( Pin, i ); delay( WAITTIME ); i = i + STEP; } i = 255; while ( i >= 0 ){ analogWrite( Pin, i ); delay( WAITTIME ); i = i - STEP; } }
Project 18 Circuit (Reading Switch States)

Project 18 Explanation (Reading Switch States)
In this project, you can learn how to use switch in your Arduino project and how it actually works. The digital output enables you to control the LED, motor and buzzer etc., switching the digital output pin between two states, HIGH (5V) and LOW (0V). You can also use the output to check the electronic components. The digital output pin can be switched to the digital input. By doing this, you can check the voltage applied to the pin with input in two ways, “LOW” and “HIGH”. These input values can be used to control other electronic components. For example, you may want to operate the LED or buzzer when the digital input is HIGH or stop it when the input is LOW.
Use the “serial monitor” when you want to check the acquired status. With Arduino, you can send the data from Arduino to a PC using the USB cable used to transfer the program from the PC. Serial communication is used to exchange the data. You can use serial communication to check the state acquired by digital input by sending it to the PC.
Specify “INPUT” in pinMode() to switch pin 7 used this time to the input mode. You can now check the pin state from the program.
The pin state specified in digitalRead() is acquired and stored in the value variable. “0” is stored if it is 0V or “1” if it is 5V.
Project 18 Code (Reading Switch States)
const int DIN_PIN = 7; void setup(){ pinMode( DIN_PIN, INPUT ); Serial.begin( 9600 ); } void loop(){ int value; value = digitalRead( DIN_PIN ); Serial.println( value ); delay( 1000 ); }
Project 19 Circuit (Reading Voltage)

Project 19 Explanation (Reading Voltage)
It is possible to read voltage by converting from digital to analog using the pins on the Arduino. After uploading the code to Coding BlockTM, open the serial port screen. Arduino microcontroller works with 5V voltage. This microcontroller 10-bit ADC can read voltages from 0V to 5V with an accuracy steps of 210 = 1024.
Use the red cable free end “T”, First, read the current voltage value from the serial port. Next, touch the “T” lead to the “A” point and read the values again. Finally, touch the “T” lead to the “B” point and observe the voltage differences.
Project 19 Code (Reading Voltage)
#define potpin A0 int value = 0; void setup() { Serial.begin(9600); Serial.println("Read value"); } void loop() { value = analogRead(potpin); float Voltage = (5.00/1024.00)*value; // Serial.println(Voltage); //The calculated voltage is sent as a message to the serial monitor. delay(1000); }
Project 20 Circuit (Flip Flop Lights)

Project 20 Explanation (Flip Flop Lights)
Flip Flop Lights; It is an application that makes two LEDs flash sequentially. While one of the LEDs is on, the other is off, and you can adjust the on-off times of the LEDs as you wish.
Build this circuit. Load sketch Flip Flop Lights into Coding BlockTM using the programming instructions below.
A program that sequentially flashes the LEDs connected to pins 2 and 3 of the Coding BlockTM, with 1 second intervals and continues in a loop.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 20 Code (Flip Flop Lights)
void setup() { pinMode(2, OUTPUT); // Set pin 2 of the Arduino as output. pinMode(3, OUTPUT); // Set pin 3 of the Arduino as output. } void loop() { digitalWrite(2, HIGH); // Make pin 2 logic 1 here. digitalWrite(3, LOW); // Make pin 3 logic 0 here. delay(1000); // wait 1 second. digitalWrite(2, LOW); // Make pin 2 logic 0 here. digitalWrite(3, HIGH); // Make pin 3 logic 1 here. delay(1000); // wait 1 second. }
Project 21 Circuit (Flip Flop Lights and Sound)

Project 21 Explanation (Flip Flop Lights and Sound)
Use the preceding circuit and replace LED (L1) with buzzer (BZ1) and run the program. While the LED (L2) is on, the buzzer (BZ1) is off. While the LED (L2) is off, the buzzer (BZ1) is on. And you can adjust the on-off times of the LED and buzzer as you wish as well.
If you wish, you can operate it independently from the computer by using the battery block (B3) and switch block (S1).
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 21 Code (Flip Flop Lights and Sound)
void setup() { pinMode(2, OUTPUT); // Set pin 2 of the Arduino as output. pinMode(3, OUTPUT); // Set pin 3 of the Arduino as output. } void loop() { digitalWrite(2, HIGH); // Make pin 2 logic 1 here. digitalWrite(3, LOW); // Make pin 3 logic 0 here. delay(1000); // wait 1 second. digitalWrite(2, LOW); // Make pin 2 logic 0 here. digitalWrite(3, HIGH); // Make pin 3 logic 1 here. delay(1000); // wait 1 second. }
Project 22 Circuit (Increasing the Light Brightness Incremental)

Project 22 Explanation (Increasing the Light Brightness Incremental)
Build the circuit and load sketch Gradually Increasing the Light Brightness into Coding BlockTM using the programming instructions. When the switch (S1) is OFF the LED (L1) will light at maximum brightness. Turn the switch ON, the brightness of the light increases in 4 steps. It keeps repeating until you turn off the switch (S1).
The LED brightness is set using the digitalWrite() command, which uses values from 0 to 255, but incremental increases will be much more noticeable between low numbers than between high numbers. Because of this, this sketch uses an array to set the LED brightness in multiples of 2, then resets back to minimum. Incremental increases will be much more noticeable when increasing 85 point relative to last value.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 22 Code (Increasing the Light Brightness Incremental)
#define led 3 //assign pin 3 to LED #define Switch 7 //assign pin 7 to Switch #define SAMPLING 250 //250milliSecond per press, used for debouncing static unsigned long TIMER = 0; //record current millis() byte state = 0; //val used to check current status of Switch later in the program byte brightness[] = {0, 75, 150, 255}; //comment this out if not picky void setup() { pinMode(led,OUTPUT); pinMode(Switch,INPUT); digitalWrite(Switch, HIGH); //use internal pull up resistor, no need for external resistor } void loop() { if(millis() - TIMER >= SAMPLING) //sampling ~= 250mS { TIMER = millis(); if(!digitalRead(Switch)) { state++; //increment state if(state > 3) //want only 4 brightness options, 0, 1, 2, 3 state = 0; analogWrite(led, brightness[state]); //if you aren't picky, use analogWrite(led, state*85); //no byte array; //0*85 = 0 //1*85 = 85 //2*85 = 170 //3*85 = 255 } } }
Project 23 Circuit (Switch Toggle LED)

Project 23 Explanation (Switch Toggle LED)
In this project we are going to learn how to toggle LED each time switch is on and off. Keep switching the switch (S1) several times and see how it works. See the change of LED’s state.
Open the serial monitor and see the messages when you turn the switch (S1) on.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 23 Code (Switch Toggle LED)
const int Switch_PIN = 7; // Arduino pin connected to switch pin const int LED_PIN = 3; // Arduino pin connected to LED's pin // variables will change: int ledState = LOW; // the current state of LED int lastSwitchState; // the previous state of Switch int currentSwitchState; // the current state of Switch void setup() { Serial.begin(9600); // initialize serial pinMode(Switch_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode currentSwitchState = digitalRead(Switch_PIN); } void loop() { lastSwitchState = currentSwitchState; // save the last state currentSwitchState = digitalRead(Switch_PIN); // read new state if(lastSwitchState == HIGH && currentSwitchState == LOW) { Serial.println("The Switch is on"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(LED_PIN, ledState); } }
Project 24 Circuit (Switch Toggle Buzzer)

Project 24 Explanation (Switch Toggle Buzzer)
This project is same as last project, just replace LED (L1) with the buzzer (BZ1). Other ways of working are same.
Open the serial monitor and see the messages when you turn the switch (S1) on.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 24 Code (Switch Toggle Buzzer)
const int Switch_PIN = 7; // Arduino pin connected to switch pin const int Buzzer_PIN = 3; // Arduino pin connected to Buzzer's pin // variables will change: int BuzzerState = LOW; // the current state of Buzzer int lastSwitchState; // the previous state of Switch int currentSwitchState; // the current state of Switch void setup() { Serial.begin(9600); // initialize serial pinMode(Switch_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(Buzzer_PIN, OUTPUT); // set arduino pin to output mode currentSwitchState = digitalRead(Switch_PIN); } void loop() { lastSwitchState = currentSwitchState; // save the last state currentSwitchState = digitalRead(Switch_PIN); // read new state if(lastSwitchState == HIGH && currentSwitchState == LOW) { Serial.println("The Switch is on"); // toggle state of Buzzer BuzzerState = !BuzzerState; // control Buzzer arccoding to the toggBuzzer state digitalWrite(Buzzer_PIN, BuzzerState); } }
Project 25 Circuit (How to Detect Short Press)

Project 25 Explanation (How to Detect Short Press)
We measure the time duration between the pressed and released events. If the duration is shorter than a defined time, the short press event is detected. You can change the SHORT_PRESS_TIME = 500 value as desired. You will se no reaction at the serial monitor when the duration time longer as you set.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 25 Code (How to Detect Short Press)
const int Switch_PIN = 7; // the number of the pushSwitch pin const int SHORT_PRESS_TIME = 500; // 500 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(Switch_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/Switch: currentState = digitalRead(Switch_PIN); if(lastState == HIGH && currentState == LOW) // Switch is on pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // Switch is off releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } // save the the last state lastState = currentState; }
Project 26 Circuit (How to Detect Long Press)

Project 26 Explanation (How to Detect Long Press)
We measure the time duration between the pressed and released events. If the duration is longer than a defined time, the long press event is detected. You can change the LONG_PRESS_TIME = 1000 value as desired. You will se no reaction at the serial monitor when the duration time shorter as you set.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 26 Code (How to Detect Long Press)
const int Switch_PIN = 7; // the number of the pushSwitch pin const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(Switch_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/Switch: currentState = digitalRead(Switch_PIN); if(lastState == HIGH && currentState == LOW) // Switch is on pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // Switch is off releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state lastState = currentState; }
Project 27 Circuit (How to Detect Long Press and Short Press)

Project 27 Explanation (How to Detect Long Press and Short Press)
References to project 25 and 26.
Why Needs Long Press and Short Press. To save the number of buttons. A single button can keep two or more functionalities. For example, short press for changing operation mode, long press for turn off the device. Use of long press to reduce the short press by accident. For example, some kinds of devices use the button for factory reset. If the button is pressed by accident, it is dangerous. To avoid it, the device is implemented to be factory reset only when the button is long-press (e.g over 5 seconds)
See the result on Serial Monitor!
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 27 Code (How to Detect Long Press and Short Press)
const int Switch_PIN = 7; // the number of the pushSwitch pin const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin unsigned long pressedTime = 0; unsigned long releasedTime = 0; void setup() { Serial.begin(9600); pinMode(Switch_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/Switch: currentState = digitalRead(Switch_PIN); if(lastState == HIGH && currentState == LOW) // Switch is on pressedTime = millis(); else if(lastState == LOW && currentState == HIGH) { // Switch is off releasedTime = millis(); long pressDuration = releasedTime - pressedTime; if( pressDuration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); if( pressDuration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state lastState = currentState; }
Project 28 Circuit (Using Arduino IDE Tabs)

Project 28 Explanation (Using Arduino IDE Tabs)
In this project make sound if switch (S1) is on. Load the sketch to the Coding BlockTM below. The melody will play till to end even you switched the circuit off.
By using Arduino IDE tabs, copy the code from the first block called: Arduino Code Melody Tab
You need an additional library to use the program called pitches.h
Then open a new tab and give it this name: pitches.h than click ok button.
The Tab looks like this.
For using Arduino IDE tabs, copy the code from the second block called: pitches.h Library Tab and load the code to Coding BlockTM. Turn the switch (S1) on to listen the melody.
Please Note: If you have BC-135 (SKU: 86600) Mechabau® Block Circuits® Set you can use the Press Switch (S2) instead sliding switch (S1).
Project 28 Code (Using Arduino IDE Tabs)/Arduino Code Melody Tab
#include "pitches.h" const int Switch_PIN = 7; // Arduino pin connected to Switch's pin const int BUZZER_PIN = 3; // Arduino pin connected to Buzzer's pin // notes in the melody: int melody[] = { NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_G5 }; // note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo: int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4 }; void setup() { Serial.begin(9600); // initialize serial pinMode(Switch_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode } void loop() { int SwitchState = digitalRead(Switch_PIN); // read new state if (SwitchState == LOW) { // Switch is pressed Serial.println("The Switch is being pressed"); buzzer(); } } void buzzer() { // iterate over the notes of the melody: int size = sizeof(noteDurations) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZER_PIN, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(BUZZER_PIN); } }
Project 28 Code (Using Arduino IDE Tabs)/pitches.h Library Tab
// pitches.h Library Tab #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978
Project 29 Circuit (Change the Sound Frequency According to the Light Intensity)

Project 29 Explanation (Change the Sound Frequency According to the Light Intensity)
In this project, we will learn to experiment with LDR and a buzzer. The output of this experiment is sound with different frequencies that are produced depending upon the light falling on the LDR/photoresistor. Now create the circuit and load the code below to the Coding BlockTM. Turn the switch (S1) ON and move your hand over the LDR/photoresistor (PR1). Pay attention to the change in sound frequency.
Project 29 Code (Change the Sound Frequency According to the Light Intensity)
int Buzzer = 9; // The buzzer is connected to the digital pin 9 int photoresistor = 0; // The photoresistor is connected to the analog pin 0 int Note; // Variable in which the note value is saved void setup () { pinMode (Buzzer, OUTPUT); // The pin to which the buzzer is connected is set as an output } void loop () { Note = 3 * (analogRead (photoresistor)) + 400; // Calculation of the note tone (Buzzer, Note); // Writing the note on pin 9 delay (10); // Wait 10 ms before changing the note }
Project 30 Circuit (Change the Beep Frequency Depending on Light)

Project 30 Explanation (Change the Beep Frequency Depending on Light)
In this experiment we are going to work with a sensor which is a resistor (R1) that depends on light like last project. But in this lesson we will change the beeping frequency of buzzer (BZ1) by starting to turn ON the switch (S1). In a dark environment, the resistor will have a very high resistance. As light land on the detector (PR1), the resistance will decrease. The more light is will have a lower resistance. By reading different values from the sensor, we can detect if is it light , dark or a value between them.
Turn the switch (S1) ON and move your phonelight etc. over the LDR/photoresistor (PR1). Pay attention to the change in beeping frequency.
Project 30 Code (Change the Beep Frequency Depending on Light)
int piezoPin = 9; // Declaring Piezo Buzzer on Pin 9 int ldrPin = A0; // Declaring LDR on Analog Pin A0 int ldrValue = A0; // Reading different values from the LDR void setup() { } void loop() { // Starting the cycle functions below ldrValue = analogRead(ldrPin); // read the value from the LDR tone(piezoPin,750); // Play a 750Hz tone from the piezo (beep) delay(10); // wait a bit, change the delay for fast response. noTone(piezoPin); // stop the tone after 10 ms in this case delay(ldrValue); // wait the amount of milliseconds in ldrValue } // End of cycle functions
Please contact us for any questions or project suggestions within the scope of the above Mechabau® Block Circuits® / Coding Projects.