Implementing PWM with Raspberry Pi Pico using MicroPython

Hello readers, I hope you all are doing great. In our previous tutorial, we discussed the implementation of LED interfacing and blinking program with Raspberry Pi Pico using MicroPython programming language. So continuing with our previous tutorial, in this tutorial we will learn how to control the LED brightness using PWM (pulse width modulation technique).

As we mentioned above, in our previous tutorial we implemented the LED blinking program with a Raspberry Pi Pico board. Blinking an LED means turning ON and OFF and thus the process involves only two states that are ‘1’ (HIGH) and ‘0’ (LOW). But, sometimes it is required to change the digital output between these two (ON and OFF states) for example changing the LED brightness. PWM or Pulse Width Modulation technique is used to change the brightness of the LED.

Raspberry Pi Pico PWM

Raspberry Pi Pico (RP2040) offers 40 pins which include power supply pins, General Purpose Input Output (GPIOs) pins, ADC, PWM etc. where most of the pins are multifunctional except power supply pins. Almost all the GPIO pins (30 pins) can be used to implement pulse width modulation technology.

Fig. 1 Raspberry Pi Pico Pi-Out

RP2040 offers 16 PWM channels and each channel is independent of another. Means we can set the duty cycle and frequency for one channels without affecting the others. These PWM channels are represented in the form of slices or groups and each slice contains two PWM outputs channel (A/B). There are total 8 slices and hence 16 channels. The pin B from PWM (A/B) can also be used as an input pin for duty cycle and frequency measurement.

The PWM channel provided in RP2040 are of 16 bit resolution which means maximum PWM resolution is 2^16 or ranges between 0 to 65536.

For more information on Raspberry Pi Pico’s PWM channels you can visit the Raspberry Pi organization’s official website: https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf

 

Before programming the Raspberry Pi Pico for PWM implementation let’s first understand the concept of Pulse Width Modulation.

PWM

Fig. 2 Pulse width modulated signal

Pulse Width Modulation (or PWM) is a modulation technique used for controlling the power delivered to a load or external/peripheral devices without causing any power loss by pulsing the direct current (DC) and varying the ON time of the digital pulse. Using a digital input signal, the Pulse Width Modulation technique produces modulated output signals.

The following factors influence the behaviour of a pulse width modulated signal:

  1. Frequency
  2. Duty Cycle
  3. Resolution

Frequency : Technically, frequency means “number of cycles per second”. When we toggle a digital signal (ON and OFF) at a high frequency, the result is an analog signal with a constant voltage level.

The number of cycles per second defined by a signal's frequency is inversely proportional to the time period. PWM frequency is determined by the clock source. The frequency and resolution of PWM are inversely proportional.

Duty Cycle : It is the ratio of ON time (when the signal is high) to the total time taken to complete the cycle.

Duty Cycle =

  • The duty cycle is represented in the form of percentage (%) or ratio.

Fig. 3 Duty cycle

Resolution: A PWM signal's resolution refers to the number of steps it can take from zero to full power. The resolution of the PWM signal can be changed. For example, the Raspberry Pi Pico module has a 1- 16 bit resolution, which means we can set up to 65536 steps from zero to full power.

Various applications of Pulse Width Modulation technique are:

  • Speed control of DC motor
  • Direction or position control of a servo motor
  • To control the LED brightness
  • Loudness control in buzzers
  • Measurements purpose
  • Controlling the fan speed

Software and Hardware Components Required

  • Raspberry Pi Pico Module
  • Latest version of Python
  • Latest version on Thonny IDE should be installed on your system
  • MicroPython setup installed in Raspberry Pi Pico
  • Breadboard
  • LED
  • Resistor (330 ohm)
  • USB cable

We have already published tutorials on how to download and install the above-mentioned software components.

Follow the given link for a detailed study of Raspberry Pi Pico: https://www.theengineeringprojects.com/2022/04/getting-started-with-raspberry-pi-pico.html

Programming Raspberry Pi Pico with MicroPython

To program the Raspberry Pi Pico board there are various development environments available (like uPyCraft IDE, Visual Studio Code, Thonny IDE ect.) and multiple programming languages as well.

In this tutorial, we are going to use Thonny IDE to program the Raspberry Pi Pico board.

Installing Thonny IDE for Raspberry Pi Pico Programming:

We have already published a tutorial on installing Thonny IDe for Raspberry Pi Pico Programming. Follow the given link to install the IDE: https://www.theengineeringprojects.com/2022/04/installing-thonny-ide-for-raspberry-pi-pico-programming.html

Steps to write a program for LED brightness conrtol are:

  • Open the installed Thonny IDE for all users.
  • Connect the Raspberry Pi Pico development board with your system (laptop or desktop).
  • To create a new project; go to Files >> New.

Fig. 4 New Project

  • Connect the Raspberry Pi Pico board with laptop/desktop using USB cable.
  • Click on the ‘MicoPython(Raspberry Pi Pico)’ option from lower right corner in-order to select the interpreter for raspberry Pi Pico programming using MicroPython programming language. We already selected the one from lower right corner menu from the Thonny IDE window.

Fig. 5 Select Interpreter

  • Select interpreter ‘MicroPython (Raspberry Pi Pico)’ for Raspberry Pi Pico programming.

Image: 6 MicroPython for raspberry Pi Pico programming

  • You should see this MicroPython Version details in Shell section (to ensure that Thonny is ready to program your Raspberry Pi Pico board) as shown below:

Image: 7 Ready to program

MicroPython program to change the brightness of peripheral LED.

In this example code we will implement the pulse width modulation on the digital output pin (GPIO 14). The brightness of the will vary from minimum to maximum brightness back and forth. The maximum possible resolution is 16 bit that is 2^16 (or 0 – 65535). Let’s write and understand the MicroPython program for PWM implementation:

The first task is importing library files necessary for PWM implementation. We are importing two libraries first one is to access the GPIO pins and another one is to implement pulse width modulation techniques. We also need to import ‘time’ library file to add some delay.

Fig. 8 Import libraries

To define the GPIO pin to be used as a PWM pin a ‘PWM()’ function is used which further contains the ‘Pin()’ function that is passing the PWM GPIO (14) pin number.

The PWM function is further represented using a ‘led’ object. If you are not familiar with the PWM pin details like to which slice and channel the pwm pin belongs, you can get the respective details using print(led) function.

Fig. 9 declare object

The led.freq() command is used to set frequency rate at which the digital pulse will be modulated.

Fig. 10 Frequency of modulation

Inside the while loop we are using two for() loops. First one to change the LED brightness from minimum to maximum resolution (‘0’ to ‘65525’ ).

Fig. 11

Another for loop() is used to set the LED brightness from maximum resolution (65535) to ‘0’ (minimum resolution).

The process will be repeated back and forth due to the ‘while()’ loop.

Fig. 12

Save and test the PWM program

    1. Click on the ‘save’ icon to save the PWM program.

Fig. 13 Save the program

  1. After clicking on the ‘Save’ icon, an option ‘Where to save to?’ will pop-up. You can select any of the ‘Raspberry Pi Pico’ option.

Fig. 14 Save the program

  1. Run the program once it is successfully saved to change the LED brightness by clicking on the ‘Run’ icon as shown below:

Fig. 15 Run the saved program

Code

from machine import Pin, PWM

import time

led = PWM(Pin(14))

print(led)

led.freq(1000)

while True:

for duty in range(0, 65535):

led.duty_u16(duty)

print(duty)

time.sleep(0.001)

for duty in range(65535, 1):

led.duty_u16(duty)

print(duty)

time.sleep(0.001)

Testing and Results

Let’s interface a peripheral LED with raspberry Pi Pico. As mentioned in the code description the GPIO 14 pin is configured as PWM pin. Connect the LED with raspberry Pi Pico board. The connections of LED with Raspberry Pi Pico board are shown in Table 1.

Table 1

Schematic of LED interfacing with raspberry Pi Pico is shown below:

Image: 16 LED Interfacing with Raspberry Pi Pico

  • The results of the pulse width modulation implementation on LED are shown in the images attached below.
  • In the first image the LED brightness is very low.

Fig. 17 Brightness level 1

  • In the second image the LED brightness is slightly increased than the previous one.

Fig. 18 Brightness level 2

  • The third image represents the maximum brightness level.

Fig. 19 Brightness level 3

  • If you are not familiar with the PMW pin details we can the details of respective pwm pin using print command ‘pritn(“enter the pwm pin here”)’.
  • The image attached below represents the PWM pin details like to which slice and channel the PWM pin (GPIO 14) belongs.
  • As we can see from the image attached below that PWM slice is ‘7’ and channel is ‘0’.

Fig. 20 PWM pin details

  • We can also visualize the PWM output on Thonny IDE.
  • In Thonny IDE there is a ‘plotter’ feature available for graphical representation.
  • To enable the plotter feature, go to View and check the plotter option.

Fig. 21

  • Now you should see the plotter section on the right side of Shell

Fig. 22 Plotter

  • The duty cycle is printed in both Shell and Plotter using print()
  • The image attached below, represents the pulse width modulated output observed from the GPIO 14 PWM pin during rising edge (0 to 65535) or while the LED brightness increasing.

Fig. 23 Rising PWM output 0 to 65535 (brightness increasing)

  • Pulse width modulated output during falling edge or decreasing LED brightness (65535 to 0) is shown in the image attached below:

Fig. 24 PWM output maximum to 0

Implementing Pulse Width modulation with multiple LEDs

Now let’s take another example where we will interface multiple LEDs (16) with Raspberry Pi Pico board and then will implement pulse width modulation on those LEDs.

The procedure of interfacing and programming with Thonny IDE using MicroPython will remain similar to the previous example.

Fig. 25 Interfacing Multiple LEDs with Pico

Let’s write the code:

Code

from machine import Pin, PWM

import time

led_1 = PWM(Pin(5)) # declaring led_x object for PWM pins

led_2 = PWM(Pin(6))

led_3 = PWM(Pin(8))

led_4 = PWM(Pin(9))

led_5 = PWM(Pin(10))

led_6 = PWM(Pin(13))

led_7 = PWM(Pin(14))

led_8 = PWM(Pin(15))

led_9 = PWM(Pin(16))

led_10 = PWM(Pin(17))

led_11 = PWM(Pin(18))

led_12 = PWM(Pin(19))

led_13 = PWM(Pin(20))

led_14 = PWM(Pin(21))

led_15 = PWM(Pin(22))

led_16 = PWM(Pin(26))

print(led_1)

def led_freq(x):

led_1.freq(x)

led_2.freq(x)

led_3.freq(x)

led_4.freq(x)

led_5.freq(x)

led_6.freq(x)

led_6.freq(x)

led_7.freq(x)

led_8.freq(x)

led_9.freq(x)

led_10.freq(x)

led_11.freq(x)

led_12.freq(x)

led_13.freq(x)

led_14.freq(x)

led_15.freq(x)

led_16.freq(x)

led_freq(1000) # setting pulse width modulation prequency

while True:

for duty in range(0, 65535): # Increasing LED broghtness

led_1.duty_u16(duty)

led_2.duty_u16(duty)

led_3.duty_u16(duty)

led_4.duty_u16(duty)

led_5.duty_u16(duty)

led_6.duty_u16(duty)

led_7.duty_u16(duty)

led_8.duty_u16(duty)

led_9.duty_u16(duty)

led_10.duty_u16(duty)

led_11.duty_u16(duty)

led_12.duty_u16(duty)

led_13.duty_u16(duty)

led_14.duty_u16(duty)

led_15.duty_u16(duty)

led_16.duty_u16(duty)

print(duty) # Print the duty Cycle

time.sleep(0.001)

for duty in range(65535, 0): # deccresing LED brightness

led_1.duty_u16(duty)

led_2.duty_u16(duty)

led_3.duty_u16(duty)

led_4.duty_u16(duty)

led_5.duty_u16(duty)

led_6.duty_u16(duty)

led_7.duty_u16(duty)

led_8.duty_u16(duty)

led_9.duty_u16(duty)

led_10.duty_u16(duty)

led_11.duty_u16(duty)

led_12.duty_u16(duty)

led_13.duty_u16(duty)

led_14.duty_u16(duty)

led_15.duty_u16(duty)

led_16.duty_u16(duty)

print(duty)

time.sleep(0.001)

Code Description

As we mentioned in the introduction part raspberry Pi Pico has 8 PWM slices and 16 PWM channels. So in this example code we are interfacing 16 LEDs with PWM pins.

Most of the code instructions and commands are similar to the previous example. Here we declare 16 different led objects for 16 different GPIO pins (PWM pins).

Fig. 26 Declaring ‘led’ object

  • Next we created a function ‘led_freq()’ to set the frequency at which pulse width modulation takes place.

Fig.27 PWM frequency

  • The programming part of increasing the and decreasing the brightness of LEDs is similar to the previous example.

Result

In the images attached below, we can see the variation in LED brightness.

Fig. 28 Minimum Brightness Level

Fig. 29 Intermediate Brightness Level

Fig. 30 Maximum Brightness Level

Conclusion

In this tutorial we demonstrated the implementation of pulse width modulation on LEDs with Raspberry Pi Pico module and MicroPython programming language.

Continuing with this tutorial, in our upcoming tutorial we will discuss the interfacing of a servo-motor with raspberry pi Pico and we will also implement PWM on servo motor to control the direction of rotation.

This concludes the tutorial, we hope you find this of some help and also hope to see you soon with a new tutorial on Raspberry Pi Pico programming.

Top Reasons for Microsoft SQL Database Failure 

Today all businesses utilize databases for practically all operations, ranging from sales & marketing to CRM. Hence, whenever organizations face a database failure, they are susceptible to losing crucial data that may be costly for the entire business. Microsoft SQL is a popular RDMS in all firms or organizations for professionally managing valuable data. SQLServer is incredibly useful in the business circuit, yet it has some corruption issues and errors that DBAs need to tackle strategically.

Because of SQL database errors, users are unable to access their valuable data, and it is their responsibility to fix the issues promptly to make sure that there are no severe data loss issues. There are some predominant database corruption errors and issues. You may promptly resolve them to avoid undesirable severe data loss. We will know more about database corruption errors, issues, and perfect solutions for fixing SQL server database problems.

Causes of SQL Database Corruption 

Usually, there are three main reasons behind database failure. Let us examine each of them and identify ways to recover and restore your lost data.

File System Damage

If a computer or server is shut down improperly, or if it encounters a power outage or surge or something occurs that abruptly interrupts the process of writing data to the files, there are chances of the OS files getting corrupted or damaged severely. This may lead to critical data loss. After encountering such a crisis, you are advised to repair or at least, examine the filesystem to safeguard against any data loss in the future. For modifying or mounting a file system, a repair may be necessary. Even though a thorough check may not report or reveal any severe damage, you might still need to repair the filesystem.

File Corruption

According to Small Business Chron, organizations of all sizes depend on SQLServer database from Microsoft for organizing and securing critical information. If one or even more database files are corrupted or damaged, they may result in database failure at your file-level leading to corruption. Files may be damaged because of numerous reasons. Primary files that are capable of corrupting the entire database, may themselves get corrupted because of any SQLServer account changes, file header corruption, and accidental data deletion, among many others. In case of corruption in secondary files, the SQL database will be inaccessible. When you attempt to access the damaged database, you will be getting different types of error messages. You may be several solutions for effectively repairing a corrupt database, but these solutions will not guarantee or assure you that there will be100 percent data recovery. You may consider using the log files for restoring the database. But remember that the log files are not adequate t help recover lost data in corruption cases since often backup files may get damaged if corruption seems to be severe.

Another effective way of repairing the corrupt or compromised database is by utilizing database console commands like CHECKDB, DBCC, or DBCC DEBREPAIR. These commands could help fix minor database issues. However, they are not efficient enough to tackle or resolve critical corruption cases.

Hardware & Software Failure

Hardware failures will be including memory issues, bad disk sectors, disk crashes, and even disk with errors, among many other issues. Hardware failures could be because of design errors, overloading, and poor quality control at the time of fabrication, and degradation of mechanical parts. Software failures will be including issues associated with software like DBMS software, operating system, application programs, etc.

Irrespective of how trustworthy, your DBMS seems, it is not in a position to guarantee a failure-free seamless transaction each time. To prevent any kind of database failure, the most logical and proactive step is to consult with expert DBAs. Once the overall database health is analyzed, you may rest assured to avoid serious problems down the line.

Avoiding SQL Database Corruption: Some Expert Tips

  • Do not consider rebooting the server
  • During any ongoing script execution or other running database, jobs avoid abrupt shutting down the SQLServer.
  • Always keep proper backup and then upgrade your SQLServer
  • Don’t connect or re-connect the database.

Users should regularly back up their SQL database to restore the database, in case of corruption or damage. Backup is certainly the most effective way of dealing with corruption. Keep in mind that some data may be missing between the time when disaster struck and the last backup. You may consider using commands like DBCC CHECKDB to examine and resolve corruption issues. However, you may fail to repair and restore severely corrupted files using this command.

Conclusion

You may use SQL data recovery tools recommended by experts to repair and resolve corrupt database file issues due to a lack of a backup. Some cutting-edge tools are helpful in the recovery of deleted objects, and they even display a preview. SQL recovery could prove to be a daunting and stressful job. However, you may use some advanced SQL recovery tools for getting your databases back into action!

Build a GPIO Soundboard in Raspberry Pi 4

Welcome to the next tutorial of our Raspberry Pi programming course. In our previous tutorial, we learned how to create a timelapse video with still images and understand how phototimer and FFmpeg work. In this lesson, you'll make a button-controlled "music box" that plays different sounds depending on which buttons are pressed.

Where To Buy?
No.ComponentsDistributorLink To Buy
1BreadboardAmazonBuy Now
2Jumper WiresAmazonBuy Now
3Raspberry Pi 4AmazonBuy Now

What you will learn

Connect button pushes to function calls using the Python gpiozero package and uses the Python dictionary data structure

Components

  • Raspberry Pi
  • Breadboard
  • Buttons
  • Jumper wires
  • Speaker

Set up your project

For this project, you'll need some audio samples. On Raspbian, there are many audio files; however, playing them with Python can be challenging. You can, however, transform the audio files to a more simply used format for Python.

Please make a new folder and rename it because all of your project files will be saved in the new location.

Copy the sample sounds

Create a new folder named samples using the same technique as before in your new folder.

In /usr/share/sonic-pi/sample, you'll find a bunch of sample sounds. These sounds will be copied into the samples folder in the following phase.

Open the command window by selecting the icon in the upper left corner of your screen.

To transfer all items from one folder to another, execute the following lines:

You should now see all the .flac audio files inside the sample folder.

Convert the sound files

To use Python to play sound files, you must first convert them from .flac to .wav format.

Go to the sample folder inside a terminal.

How can we converting the media files?

FFmpeg is a software program that can quickly transcode video files on Raspberry Pi. This comes preloaded on the most recent Raspbian releases.

You may use this simple command to convert music or movie files format:

To transform an audio (.wav) to a mp3 format (.mp3), for example, type:

Running batch operations on a file using bash

What are batch operations?

A batch operation is a process for processing or treating a large quantity of material. When creating modest amounts of goods, the batch operation is preferred. Because this procedure provides superior traceability and flexibility, it is highly prevalent in pharmaceutical and specialized chemical manufacturing.

It's simple to rename a file with bash. For example, you may use the mv command.

But what if you had a thousand files to rename?

When you run a script or a series of commands on several files, this is known as a batch operation.

  • Try your first simple batch process with any directory containing a few files. The first thing you should do is try something simple.

The first step is to notify bash that you wish to work with all the files inside the folder.

  • f will be used to denote each directory listing inside the folder individually.
  • After that, you must instruct bash which operations to perform on each file. The file name should be echoed to standard output in this scenario.

The dollar symbol indicates that you're referring to f in this case. Finally, inform bash that you're finished.

If you press enter after each statement, bash will wait until you input done before running the loop; therefore, the command would appear as shown below:

You might also use a semi-colon to separate the instructions rather than pressing Return after every line.

How do we manipulate strings?

The process of processing and analyzing strings is referred to as string manipulation. It entails several processes involving altering and processing strings to utilize and change their data.

That final command was meaningless, but batch operations can accomplish a lot more. You can use the following command to rename each file.

  • If you run ls in the terminal, you'll notice that all files will be renamed. So, how did it all come together?
  • The first portion of the f inside *.txt instructs bash to do the action on all files ($f) with the.txt extension.
  • The command does mv $f instructs bash to relocate each file. After that, you must tell bash what the new name of the relocated file is. To do so, delete the.txt extension and replace it with .md. Fortunately, bash includes an inherent operator for removing string ends. Please take a look at this sample to understand how it works.
  • At this point, you may add something more to the string's end.

As a result, the $f percent.txt.MD syntax replaces all.txt strings with .md strings. Use the hash operator instead of the % sign to remove a string from the beginning rather than the end.

Write the following lines in your terminal. All .flac files will be converted to .wav files, and the old ones will be deleted.

Based on the Raspberry version you're using, it might take a couple of minutes.

All of the new.wav files will then be visible inside the sample folder.

How do we play sounds using python?

After that, you'll begin writing Python code. You can do this with any code editor or IDE, but Mu is often an excellent option.

To begin building the components for your musical instrument, you'll need to see if Python will play any of the audio files you've copied.

To play audio files, import and initialize the pygame library.

This file should be saved in the gpio-music-box directory.

Select four audio files to utilize in your composition, such as:

Then make an object which references an audio file. Give the file a different name. Consider the following case:

For the following three sounds, create labelled objects.

Hint

Because all of your audio files are in the sample folder, the path will be as follows:

Each audio object should be given a name, for example, cymbal:

This is how your program should appear:

Please make a backup of your code and execute it. Then use the .play() function inside the shell from the code editor to play the audio.

Ensure that the speaker is functional and the volume is cranked up if you don't hear anything.

Connect your buttons

Four pushbuttons will be required, each connected to a different GPIO pin.

There are 26 GPIO pins on a Raspberry Pi. You may use them to transmit on/off pulses to/from electrical components like Led, actuators, and switches.

The GPIO pins are laid out as below.

There are extra pins for 3.3 V, 5v, and Grounded connections and a number for each pin.

Another illustration of the pin placement may be seen here. It also displays some of the unique pins that are available as options.

A figure with a quick explanation is shown below.

How can we use a button with a Raspberry Pi?

One of the basic input components is a button.

Buttons come in various shapes and sizes, with two or four legs, for example. Flying wire is commonly used to link the two-leg variants to a control system. Four-legged buttons are usually put on a breadboard.

The illustrations below illustrate how to connect a Raspberry Pi to a button. Pin 17 is indeed the input in both scenarios.

The breadboard's negative rail may be connected to a single GND, allowing all pushbuttons to share the same grounded rail.

On the breadboard, attach the four buttons.

Connect each pushbutton to a GPIO pin with a specific number. You are free to select whatever pin you prefer, but you must recall its number.

Hint

Connect one Ground pin to the breadboard's neutral rail. Then connect one of each button's legs to this rail. Finally, connect the remaining buttons' legs to separate GPIO pins.

Here's a wiring schematic that may be of use. The extra legs of the pushbuttons are connected to Pin 4, pin 17, pin 27, and Pin 10 in this case.

Play sounds at the press of a button.

A single pushbutton has been connected to pin 17 in the figure below.

The button may be used to invoke methods that don't need any arguments:

First, use Python language and the gpiozero library to configure the button.

The next step is to design a function with no parameters. This straightforward method prints the phrase Hello in the terminal.

Finally, build a function-calling trigger.

You can now see Hello displayed in the terminal each time that button is clicked.

You may make your function as complicated as you want it to be, and you can also call methods which are part of modules. In this case, hitting the button causes an LED in pin 4 to turn on.

The application should execute a function like drum.play() whenever the button is pushed.

However, brackets are not used when using an action (like a button push) to invoke a function

. The software must call that method whenever the button is pushed, not immediately. All you have to do is use drum.play in this situation.

First, establish one of the buttons. Ensure to replace the numbers from the example with the ones of the pins you've used.

Add the following line of code at the end of your script to play the audio whenever the button is pushed:

Push the button after running the software. If you don't hear the sound, double-check your button's connections.

Add code to the three remaining buttons to have them play their audio.

For the second button, you may use the code below.

How can we improve our script?

Good code is clear, intelligible, tested, never overly convoluted, and does the task at hand.

You should be able to run your code with no issues. However, once you have a working prototype, it's typically a good practice to tidy up your code.

Subsequent stages are entirely optional. If you're satisfied with your writing, leave it alone. Follow the instructions on this page and make your code a little cleaner.

Instead of creating eight individual objects, you may keep your pushbutton objects and audio in a dictionary. These are, nonetheless, the seven characteristics of good code.

  1. Readability

If you're writing one-time discard code that no one, including yourself, will have to see in the future, you may write in any way you like. However, the majority of useful software that has been produced has to be updated regularly.

  1. Scalable

The next important feature of excellent programming is scalability, or the capacity to expand with the demands of your organization. Scalability is primarily concerned with the code's efficiency. Scalable code doesn't always require frequent design changes to maintain performance and resolve various workloads.

  1. Testable

Last-minute adjustments are unavoidable while developing software. It will be difficult to send new changes through if the code can not be rapidly and automatically tested.

  1. Meet functional requirement

Every piece of software that is built has a certain goal in mind. For persons familiar with the functionality, a code that adheres to its requirements is simple to understand. As a result, one important characteristic of excellent code is that it meets the functional requirements.

  1. Gracefully fails

Mistakes and flaws are an inherent element of software development. You can't anticipate every conceivable manufacturing case, no matter how cautious you were during the design process. You should, however, shield your application against the negative effects of such situations.

  1. Extendable

This is an important feature of excellent coding. A reusable and sustainable code is extendable. You write code and double-check that it works as expected. Extensions and related advances can be added to the feature by modules that require it.

  1. The Code Is Reusable

For every software application, reusable coding is essential and highly beneficial. It aids in the simplification of your source code and avoids redundancy. Reusable codes save time and are cheaper in the long term.

Learn how to create simple dictionaries and iterate over them by following the methods below.

First, establish a dictionary where the Buttons serve as keys and audio as values.

Whenever the pushbutton is pressed, you can loop through the dictionary to instruct the computer to play the audio:

Conclusion

We learned how to make a "music box" with buttons that play sounds depending on which button is pushed in this lesson. We also learnt how to interface our raspberry pi with buttons via the GPIO pins and wrote a python program to control the effects of pressing the buttons. In the following tutorial, we will learn how to perform a voice control on Raspberry pi.

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir