Author Topic: Getting started with microcontrollers  (Read 7720 times)

0 Members and 1 Guest are viewing this topic.

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9953
  • Country: nz
Re: Getting started with microcontrollers
« Reply #25 on: March 15, 2024, 11:38:39 am »
Thanks for all the replies. I think I will start by getting the Arduino Uno REV3 and go from there. I should have no problem interfacing hardware as I am very familiar with that kind of thing. The big hurdle is that I need to learn C and/or C++. I will have to find some tutorials and start practicing. We shall see if I can do it or if it proves to be more effort than I am willing to do.

Arduino is good in that it hides all the hard things away by doing them for you. You can focus on learning how to program in C without having to deal with the low level hardware control stuff.
 

Greek letter 'Psi' (not Pounds per Square Inch)
 

Online tooki

  • Super Contributor
  • ***
  • Posts: 11561
  • Country: ch
Re: Getting started with microcontrollers
« Reply #26 on: March 15, 2024, 01:16:32 pm »
Thanks for all the replies. I think I will start by getting the Arduino Uno REV3 and go from there. I should have no problem interfacing hardware as I am very familiar with that kind of thing. The big hurdle is that I need to learn C and/or C++. I will have to find some tutorials and start practicing. We shall see if I can do it or if it proves to be more effort than I am willing to do.

Arduino is good in that it hides all the hard things away by doing them for you. You can focus on learning how to program in C without having to deal with the low level hardware control stuff.
Yep. Yet it doesn’t stop you from going deeper if and when you need to. (Arduino opponents often overlook the fact that you can choose to not use the Arduino functions whenever you like.)
 

Online tooki

  • Super Contributor
  • ***
  • Posts: 11561
  • Country: ch
Re: Getting started with microcontrollers
« Reply #27 on: March 15, 2024, 01:32:24 pm »
Thanks for all the replies. I think I will start by getting the Arduino Uno REV3 and go from there. I should have no problem interfacing hardware as I am very familiar with that kind of thing. The big hurdle is that I need to learn C and/or C++. I will have to find some tutorials and start practicing. We shall see if I can do it or if it proves to be more effort than I am willing to do.
Take it from me, someone who is NOT a gifted programmer: if I can learn Arduino (and thus C/C++) and make it do nice things (and from there, also learn to program in C/C++ on other MCUs, like PIC and STM32, in other IDEs), then practically anyone who can think logically can!

An Arduino Uno R3 or a Mega 2560 (essentially identical, but with way more IO pins) is a great way to start.

“Arduino” actually encompasses three things:
1. A family of microcontroller dev boards
2. The Arduino framework, i.e. the layer of Arduino commands (which are really just shortcuts) on top of more-or-less standard C++
3. The Arduino IDE, i.e. the text editor used to write and upload the code

None of these is mandatory — you can use Arduino boards with other IDEs and frameworks; you can use the Arduino framework with tons of non-Arduino boards; you can use the Arduino framework with other IDEs. (The only exception is that the Arduino IDE only supports the Arduino framework.) Beginners typically start using all three, then often migrate to more powerful boards, but still using the Arduino IDE. More advanced users then often migrate to another IDE.

I mostly use the Arduino framework running on ESP32 boards, using the PlatformIO IDE. :) But I’ve also played around with STM32 and Rpi Pico boards, all using the Arduino framework. At work I had to design a device around a PIC, and at school we had to use STM32, but without Arduino. Again, if I can learn it, so can you!
 

Online BTO

  • Frequent Contributor
  • **
  • Posts: 257
  • Country: au
Re: Getting started with microcontrollers
« Reply #28 on: March 15, 2024, 11:16:11 pm »
Hi mate,
I have exhausted the living crap out of studying the different boards AND I CAN ADVISE YOU PRETTY RELIABLY AS FOLLOWS

1. You're a beginner , so that needs to be kept in mind
(as others have stated you don't necessarily need to use Arduino IDE for programming) now while this is correct, You should start on Arduino IDE.

2. DECIDING WHICH BOARD TO GO FOR
Well, first this a question of .. What do you want to do ? However no beginner knows what they want to do , they usually just play with LED Strips for the most part , Sure, do that for a few days, then.. Move onto some more constructive that you'll actually learn from.

So as for the Boards,  Your first basic choices are... Do you want to go down the Raspberry Pi road (i would advise NO unless you can give me a reason why not.
so LET'S NOW TALK ABOUT THE OTHER DEV BOARD OPTIONS
You have a lot of choice, TeensyDuino, Arduino, Uno, Nano ,Mega, micro etc etc then all the other boards out there

HERE IS THE SUMMARY
You have 2 basic choices,
ARDUINO UNO
ESP32 WROOM

THE BETTER CHOICE IS THE ESP32 WROOM

This thing is so cheap it's not funny
On top of that , You have Dual core processing (You will thank me for this later)
You can do everything that you can do on Arduino, on an ESP32 Board
You can use the same language, write the same program (Except ESP32 has some MINOR Modifications)  Not a big deal, ok

3. Now you don't want to be someone that just flips through sketches and loads them all the time, You want to know how to have some basic programming skills


STEP 1 - Go to this link and the do the basic programming course with the ESP32 and Arduino IDE loaded , so you can follow along , that way you'll understand program structure
and how things work.

Start here
https://startingelectronics.org/software/arduino/learn-to-program-course/01-program-structure-flow/


STEP 2 - Your basic "Bare Minimum" Code for Arduino Is going to look like this
Here is a format that i use for myself and when teaching students.
Code: [Select]
/*
 GLOBAL FUNCTIONS AND VARIABLES

///////////////////////////////////////////////////////////////////////////////////////////
 PROJECT INFORMATION
-------------------------------------------------------------------------------------------
 CREATED BY :
 CREATED BY :
 DATE       :
 PROJECT    :
 VERSION    :
 LAST EDIT  :
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
 WIRING INFORMATION
-------------------------------------------------------------------------------------------
   PIN #  - DESCRIPTION
///////////////////////////////////////////////////////////////////////////////////////////
*/
///////////////////////////////////////////////////////////////////////////////////////////
// Libraries to include
//-----------------------------------------------------------------------------------------
//#include <LIBRARY_NAME.h>             //DESCRIPTION OF LIBRARY
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN SETUP
///////////////////////////////////////////////////////////////////////////////////////////
void setup()
{  //OPENS Void Setup

///////////////////////////////////////////////////////////////////////////////////////////
}  //CLOSES Void Setup
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN LOOP
///////////////////////////////////////////////////////////////////////////////////////////
void loop()
{  //OPENS Void Loop

///////////////////////////////////////////////////////////////////////////////////////////
}  //CLOSES Void Loop
///////////////////////////////////////////////////////////////////////////////////////////


Now here is the basic structure for ESP32 (Considering that you'll now be playing with 2 Cores)

Code: [Select]
/*
 GLOBAL FUNCTIONS AND VARIABLES
///////////////////////////////////////////////////////////////////////////////////////////
 PROJECT INFORMATION
-------------------------------------------------------------------------------------------
 CREATED BY :
 CREATED BY :
 DATE       :
 PROJECT    :
 VERSION    :
 LAST EDIT  :
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
 WIRING INFORMATION
-------------------------------------------------------------------------------------------
   PIN #  - DESCRIPTION
///////////////////////////////////////////////////////////////////////////////////////////
*/
///////////////////////////////////////////////////////////////////////////////////////////
// Libraries to include
//-----------------------------------------------------------------------------------------
//#include <LIBRARY_NAME.h>             //DESCRIPTION OF LIBRARY
///////////////////////////////////////////////////////////////////////////////////////////

/*/////////////////////////////////////////////////////////////////////////////////////////
 INITIATES THE TASKS
*///---------------------------------------------------------------------------------------
void Task1(void *pvParameters);  //Initiates Task1
///////////////////////////////////////////////////////////////////////////////////////////

/*/////////////////////////////////////////////////////////////////////////////////////////
 DEFINES THE TASK HANDLES
*///---------------------------------------------------------------------------------------
TaskHandle_t Task_1;  //Handle for Task1
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN SETUP
///////////////////////////////////////////////////////////////////////////////////////////
void setup()
{  //OPENS Void Setup
/*/----------------------------------------------------------------------------------------
//SERIAL COMMUNICATION
-------------------------------------------------------------------------------------------
 Baud Rate Setup
*///---------------------------------------------------------------------------------------
  Serial.begin(115200);                           // Serial Monitor Baud Rate
  delay(10);
///////////////////////////////////////////////////////////////////////////////////////////
/*-----------------------------------------------------------------------------------------
  TASK CREATION
*///---------------------------------------------------------------------------------------
  xTaskCreate(Task1, "Task 1", 2000, NULL, 1, &Task_1);
  vTaskDelay(100);
///////////////////////////////////////////////////////////////////////////////////////////
}  //CLOSES Void Setup
///////////////////////////////////////////////////////////////////////////////////////////

/*/////////////////////////////////////////////////////////////////////////////////////////
 TASK EXECUTION - TASK 1
*//////////////////////////////////////////////////////////////////////////////////////////
void Task1(void *pvParameters)  //Initiates Task1
{ // OPENS Task1
  Serial.print(F("TASK 1 IS RUNNING ON CORE ")); 
  Serial.println(xPortGetCoreID());
  //vTaskDelay(1000);
  for(;;)
  { //OPENS  Task1 FOR Loop
    vTaskDelay(10); // REMOVE THIS LINE BEFORE INSERTING CODE
  } //CLOSES Task1 FOR Loop
} // CLOSES Task1
///////////////////////////////////////////////////////////////////////////////////////////
void loop()
{  //OPENS Void Loop
    vTaskDelay(10); // REMOVE THIS LINE BEFORE INSERTING CODE
}  //CLOSES Void Loop
///////////////////////////////////////////////////////////////////////////////////////////

Possibly the best advice i can give you is when starting ESP32 everyone is going to go on about "You have to pin the task to the core" using the function "xTaskCreatePinnedToCore"
THIS IS NOT CORRECT, Just use "xTaskCreate" as i have done here, Run it and play around with the serial monitor and noting how the rules work for the cores

OK, so this will get you started and understanding the basics

NOW, YOU'RE GOING TO ACTUALLY HAVE TO READ THE FREE RTOS USERMANUAL

chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://www.freertos.org/Documentation/Mastering-the-FreeRTOS-Real-Time-Kernel.v1.0.pdf

A QUICK CHEAT SHEET START

1. Jump straight to page 72 (Figure 4.7)   This will explain STATES OF A TASK to you
2. Now go back to page 54 , The beginning of the chapter of TASK MANAGEMENT and go to around page 93

You'll now have a very good understanding of what you're doing

3. Now make a coffee and set aside 3 days of hard study and read the entire manual.

SO HERE IS WHY
You're going to start with LED's (usually on Arduino) and when you want to blink them, you're going to be taught to use Delay as the function and a delay will be followed by a time period of the delay.   Now you'll always run into a problem BECAUSE OF HOW ARDUINO WORKS vs ESP32
the problem will be other things will be stopped and can't run until the delay time is up

The communities solution to this is to use "millis"  LOL    and.. for simple stuff. it works, (But then, for simple stuff, so does delay)
the point is, You're going to fail again. This is why FreeRTOS is better, You see, in ESP32 vTaskDelay  does not act as BLOCKING CODE (see that diagram that i showed you)
but in Arduino it does.  Write experimental code to prove this, it'll help your programming skills

Point is, ESP32 is better than Arduino

Now... To get you started, REACH OUT TO ME IF YOU HAVE ANY NEED FOR ANY OF THIS CODE
I'm going to give you a list of what i have As Test Sketches.  These were all created either for educational purposes of basic test purposes

- There is something called FastLED Pragma Message, it's an error that you can ignore, but i have code that can silence it

- Arithmetic Operations - This code helps you understand Arithmetic coding

- FOR Loops - This sketch let's you play with FOR Loops and understand how they work

- Increment Operators - This sketch let's you play with incremental operators to understand how to control the variables to do what you want to do.

- Relational Operators - This sketch let's your play around with relational operators to learn how they work

- Variables

- Test 2 LED Strips - A Sketch to test 2 LED Strips if they are working or not

- Basic Printline -  A Sketch to let you play with the output serial monitor for diagnostics

- FASTLED Multitasking - To be used with the FastLED library, a sketch to teach you multitasking on 1 core

- FastLED RGB Ordering - THIS ONE IS USEFUL - A Sketch that when you run it and you don't know the RGB ordering of the LED Strip that you just bought, this sketch will tell you the ordering of the strip,   saves you many many hours of wondering , why is the correct colour not coming up

- FOR Loops - A Sketch to help you understand FOR Loops

- Get Core ID - A sketch to help you understand which core is currently running a task

- While For Loops -  A Sketch (When you're ready and if you need to use it) that will teach you WHILE FOR loops

I won't post it all here now, but to get you doing your own code. Let me know and i'll pop it up
(Likely i'll create a new thread for it for everyone to see and you can grab the code from there.


FOR NOW MY ADVICE TO YOU IS ESP32 as a direct answer to your original question
and i wouldn't bother with those boards that you bought because those boards and arduino nano have more problems than you want to deal with
when you are starting off .


QUESTION EVERYTHING!!!
 

Online tooki

  • Super Contributor
  • ***
  • Posts: 11561
  • Country: ch
Re: Getting started with microcontrollers
« Reply #29 on: March 16, 2024, 01:25:20 am »
Let it be documented that I think BTO’s comment above could not be more wrong, in that basically everything stated is utter nonsense. Multicore programming is WAY beyond what anyone should be beginning with, and introducing that now is neither necessary nor helpful. RTOSes are in no way what one should be starting with!! The post above is a recipe for how to confuse a beginner, not help them.  :palm:  :palm:
« Last Edit: March 16, 2024, 01:28:28 am by tooki »
 
The following users thanked this post: pcprogrammer

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #30 on: March 16, 2024, 07:08:56 am »
And to add onto tooki, the readability of the code with all the "/////////" does not improve.

Adding comment sure but this is over the top  |O

So BTO  :-- :--

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: Getting started with microcontrollers
« Reply #31 on: March 16, 2024, 07:21:10 am »
STEP 2 - Your basic "Bare Minimum" Code for Arduino Is going to look like this
Here is a format that i use for myself and when teaching students.
Code: [Select]
/*
 GLOBAL FUNCTIONS AND VARIABLES

///////////////////////////////////////////////////////////////////////////////////////////
 PROJECT INFORMATION
-------------------------------------------------------------------------------------------
 CREATED BY :
 CREATED BY :
 DATE       :
 PROJECT    :
 VERSION    :
 LAST EDIT  :
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
 WIRING INFORMATION
-------------------------------------------------------------------------------------------
   PIN #  - DESCRIPTION
///////////////////////////////////////////////////////////////////////////////////////////
*/
///////////////////////////////////////////////////////////////////////////////////////////
// Libraries to include
//-----------------------------------------------------------------------------------------
//#include <LIBRARY_NAME.h>             //DESCRIPTION OF LIBRARY
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN SETUP
///////////////////////////////////////////////////////////////////////////////////////////
void setup()
{  //OPENS Void Setup

///////////////////////////////////////////////////////////////////////////////////////////
}  //CLOSES Void Setup
///////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN LOOP
///////////////////////////////////////////////////////////////////////////////////////////
void loop()
{  //OPENS Void Loop

///////////////////////////////////////////////////////////////////////////////////////////
}  //CLOSES Void Loop
///////////////////////////////////////////////////////////////////////////////////////////

You can help your students a lot to learn it just by making things much more easy with removing that spamming crap comments.  ;)

Here is how the same code should looks like:
Code: [Select]
//#include <LIBRARY_NAME.h>

void setup()
{
}

void loop()
{
}

as you can see its become small shiny and easy.
It's much more easy to read it and understand when there is no spamming crap comments. :)

if you want to add some large comment to document some details it's okay, but don't mix it with code. Just put that comment in one place, for example at the top of file and keep the code clean and free of spam.

For example:
Code: [Select]
/*

  HI THERE

                                                 
                                                 
        $$                              $$       
       $$$$$$                        $$$$$$     
       $$$$$$$$                    $$$$$$$$     
       $$$$$$$$$   $$$$$$$$$$$$$  $$$$$$$$$     
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
      !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   
     $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   
     $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
    $$$$$$$$$55((((v55555555555(((((55$$$$$$$$$ 
    $$$$$$$$((((((((((((((((((((((((((!$$$$$$$$ 
    $$$$$$$((((( (((((((((((((((((.!(((($$$$$$$ 
    $$$$$$J((((    ((((((((((((((    (((5$$$$$$ 
    $$$$$$((((  $$ !(((((((((((( $$%  ((($$$$$$ 
    $$$$$&(((( $*$$ ((((((((((((  $$  (((#$$$$$ 
    $$$$$((((  $$$$ (((((((((((.$$$$$ (((($$$$$ 
    $$$$$((((  $$$$ ((((((((((( $$$$$ (((($$$$7 
     $$$$((((  $$$$ (((((((((((($$$$$ (((!$$$$   
     $$$$$(((( $$$$ (((((((((((( $$$  ((($$$$$   
    "$$$$$((((  $$ (((((((((((((  $  (((($$$$$( 
      $$$$(((((   ((((((($(((((((   '(((($$$$   
       $$$$((((((((((((((v(((((((((((((($$$$$   
        $$$$(((((((((((!(((!(((((((((((($$$$     
         $$$$((((((((((($$$((((((((((($$$$$     
          $$$$$v((((((((((((((((((((v$$$$       
            $$$$$$$$5!((((((((!F$$$$$$$         
  $$$$         A$$$$$$$$$$$$$$$$$$$$             
   $$$$            $$$$$$$$$$$$$                 
  v  $$$          $$$$$$$$$$$$$$$               
  v  5$v$         $$$$$$$$$$$$$$$$               
      $$v        $$$$$$$$$$$$$$$$$               
       $$$$      $$$$$$$$ $$$$$$$$               
       $$$v$$$$$v$$$$$$$$ $$$$$$$$               
        $$$$$$$$$$$$$$$$$ $$$$ $$$               
         $$$$$$$$$$$ $$$$ $$$$ $$$               
            $$$$ $$$ $$$$ $$$$ $$$               
                 $$$ $$$$ $$$$ $$$               
                 $$$ $$$$ $$$$ $$$               
                 $$$ $$$$ $$$$ $$$               
                 $$$ $$$$'$$$$ $$$               
               .v$$$v$$$$v$$$$v$$$vv             
            vvvvv$$$v$$$$v$$$$v$$$vvvvvv         
         vvvvvvvv$$$v$$$$v$$$$v$$$(vvvvvvv/     
        vvvvvvvv($$$v$$$$v$$$$v$$$$vvvvvvvvv     
      |vvvvvvvvv$$$$v$$$$v$$$$v($$$$vvvvvvvvv   
      vvvvvvvv$$$$$vv$$$$v$$$$vv($$$$vvvvvvvvv   
      vvvvvvvvv%%%%vv$$$$v$$$$vv%%%%Ivvvvvvvvv   
      vvvvvvvvvv%%%%$$$$vvv$$$$%%%%vvvvvvvvvvv   
      vvvvvvvvvvv%%%$$$%vvv%%$$%%%%vvvvvvvvvvv   
       vvvvvvvvvv%%%v%%%%v&%%%v%%%vvvvvvvvvv"   
         vvvvvvvv%%%v#%%%v%%%%v%%%vvvvvvvvv     
           vvvvvv%%%v7%%%v%%%%v%%%vvvvvv(       
              .vv%%%v7%%%v%%%%v%%%vvv           
                     (#%%v%%%#"                 
                                                 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OWNED BY:
REVIEWED BY:
APPROVED BY:
REJECTED BY:
THE IDEA BY:
CREATED BY :
ASSISTED BY :
ART BY:
COMMENTS BY:
CODED BY:
SPONSORED BY:
DONATED BY:
HATED BY:
LIKED BY:
IGNORED BY:
DATE:
TIME:
GPS POSITION:
PROJECT    :
CODE NAME:
SUB PROJECT:
VERSION    :
SUB VERSION:
FIRST EDIT:
SECOND EDIT:
THIRD EDIT:
LAST EDIT  :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

*/
//#include <LIBRARY_NAME.h>

void setup()
{
}

void loop()
{
}

as you can see, you can add some text garbage, but still keep the code clean and simple if you don't mix it with that text garbage.  :)
« Last Edit: March 16, 2024, 10:50:06 am by radiolistener »
 

Offline zilp

  • Regular Contributor
  • *
  • Posts: 206
  • Country: de
Re: Getting started with microcontrollers
« Reply #32 on: March 16, 2024, 07:44:26 am »
I think that BTO's advice is bad for one simple reason: If you want to primarily learn programming, then doing so with microcontrollers just complicates things for no gain. If you just want to learn how to write software on top of an operating system, you can do that on Linux on your desktop/laptop, no need to fiddle around with additional hardware.

If you want to learn how to effectively use microcontrollers, though, then an operating system just obscures things. If you want to learn what microcontrollers can do, you will need to dig into datasheets and figure out how to set up registers so that the thing does what you want it to do. And for that purpose, I would think that a small AVR is a way better choice than an ESP32, simply because of its relative simplicity.

An RTOS is a useful abstraction for building embedded software that exceeds a certain level of complexity, but if you want to make effective use of microcontrollers, you need to know how you would do things without an operating system first (which in particular means that you understand how the RTOS does the things that it does).
 
The following users thanked this post: tooki

Online soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3167
  • Country: es
Re: Getting started with microcontrollers
« Reply #33 on: March 16, 2024, 08:02:26 am »
Thanks all. I am going to order the Arduino Uno and try to get started with that.

I think I might try getting my 13 year old nephew interested in learning. If he takes to it then it would motivate me and be a learning experience for him. I will have to find some learning projects which would be interesting to him.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #34 on: March 16, 2024, 08:26:51 am »
Here is how the same code should looks like:
Code: [Select]
//#include <LIBRARY_NAME.h>

void setup()
{
}

void loop()
{
}

I myself use "//------------------------------" between functions to make some distinction and add comments on input and return values.

Code: [Select]
//------------------------------------------------------------------
//Returns the current status of the pin
//port: 0 = GPIOA, 1 = GPIOB
//pin:  0 - 7

int readpin(int port, int pin)
{
}

//------------------------------------------------------------------
//Sets the pin
//state: 0 - 1
//port:  0 = GPIOA, 1 = GPIOB
//pin:   0 - 7

void setpin(int port, int pin, int state)
{
}


Less intrusive then the "///////////////////////////////////////////////////////////////////////" and still providing useful information.

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: Getting started with microcontrollers
« Reply #35 on: March 16, 2024, 08:44:00 am »
I myself use "//------------------------------" between functions to make some distinction and add comments on input and return values.

This is c++ builder style. It leads to very bad code readability, especially if you're working on a large and complicated code.

For example most modern code editors allows to collapse functions body, but if your code is contaminated with these ----------------------------, your eyes will ripple and it will be difficult to read such code.


When you collapse bodies for my code it will looks like this:
Code: [Select]
//#include <LIBRARY_NAME.h>

void setup()
void loop()
void func1()
void func2()
void func3()

Clean, tiny, simple and easy to read and navigate.


But if you collapse bodies for code separated with -----, you will get this:
Code: [Select]
//#include <LIBRARY_NAME.h>

//========================================================================================================

void setup()

//========================================================================================================

void loop()

//========================================================================================================

void func1()

//========================================================================================================

void func2()

//========================================================================================================

void func3()

//========================================================================================================

what is better readable?  ;)
« Last Edit: March 16, 2024, 08:52:20 am by radiolistener »
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 6203
  • Country: ro
Re: Getting started with microcontrollers
« Reply #36 on: March 16, 2024, 08:56:03 am »
OK, I have it [an Arduino UNO R3 board], I plug in a power supply (5V I assume) and I pug in the USB to my Linux Mint computer.  Now what? I assume i need to download some programming software. Yes?
Download and install the Arduino IDE (Integrated Development Environment) for Linux:  https://www.arduino.cc/en/software

Then what? Can you give me a step by step intro on how I would do a simple task?
Plug the UNO board into an USB port (it will get its power from USB, no need for external power supply), start the Arduino IDE, and inside the IDE select the type of board and the port where your UNO is connected.  If you manage to do that by simply inspecting the IDE menus, next step is to load an example (also from the IDE menus), compile, download it into the UNO board and enjoy.  If it is not clear how to do all that, start reading the getting started guide https://docs.arduino.cc/software/ide-v2/tutorials/getting-started-ide-v2/ or watch a video, depends of your learning style.

Also, once programmed with USB I am assuming the Arduino is totally autonomous (as long as it has power). Yes?
Yes.

While the USB is connected does it get power from there?
Yes.  There is also a jack barrel for an external power supply, read the User Guide for your specific type of Arduino board.

The main question is how is it programmed? What language does Arduino use? 
C (the most common), or a combination of C/C++ (Arduino IDE under the hood is using AVR-GCC and it supports mixed C/C++ code).  Can be programmed in AVR Assembler language, too.  In the microcontoller's datasheet there are often code excerpts in both assembler and C, for how to control the MCU, might be very educational (though, those code excerpts are not standalone/fully working examples for beginners).

Where can I find simple tutorials with simple projects for learning and exercise?
Inside the IDE itself there is a ton of C examples.  You can start from running the examples, then try to make little modification and run again.  Apart from those examples, there are all kind of Arduino getting started guides, tutorials, videos, books, etc.

Suppose I want to do a simple task like implement an AND function: read two switches and activate an output when both are closed. How would I do that?
- Read the board user guide, to find out the pinout.  Same pin can be either analog or digital, either input or output.  Your program tells it what you want it to be (there are some restrictions, but the idea is that it is all programable, not a fixed functionality like in classic microprocessors, or in digital TTL chips).  In an MCU a pin can be whatever you want it to be (well, with some restrictions).
- Choose 2 digital inputs to connect the switches.
- Choose another digital output for the LED.  On most Arduino boards, there is already an onboard LED connected to one of the pins.  Use that one, or choose some other pin and connect your own LED there, with a resistor.
- Now open the IDE, and write a program, where you read the digital input pins (the switches) and turn on the LED (write a digital 1 to the LED) when both switches are pressed.  You'll need to put all that inside an infinite loop, so the MCU forever reads the switches, evaluates if both are pressed, and turns the led ON or OFF accordingly.

So, in an MCU, your AND won't be a physical AND, like it would be when you wire a 74N08 gate.  In an MCU, this AND "gate" is in fact a program, a forever loop of reading the switches -> evaluate if both pressed -> update the LED status -> repeat.  For this approach, the response of the output LED (the "speed" of the AND "gate") is dictated by how often/how fast that program loop is running.
« Last Edit: March 16, 2024, 09:18:16 am by RoGeorge »
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: Getting started with microcontrollers
« Reply #37 on: March 16, 2024, 09:10:03 am »
Where can I find simple tutorials with simple projects for learning and exercise?

Usually programming learning is started from hello world code. For MCU it is started from a simple LED blinking code. Then you can add more actions in your code and learn it step by step. For example you can add 10 LEDS instead of 1 and do moving light effect. Then you can add LCD display and show the text on it. Then add some temperature/pressure sensor like BME280, obtain it's data and show on the LDC display. etc

You can find code examples on github.
« Last Edit: March 16, 2024, 09:13:42 am by radiolistener »
 

Online Aldo22

  • Frequent Contributor
  • **
  • Posts: 695
  • Country: ch
Re: Getting started with microcontrollers
« Reply #38 on: March 16, 2024, 09:29:43 am »
A major advantage of ESPs is that they can be controlled via a web interface.
Basically, you don't need to buy any special displays, buttons, encoders or keypads, but you can control and view everything via a web browser.
This makes it quicker to test a code.
This may not be "Hello World" level, but for a somewhat larger project you will soon decide in favor of the ESP32 if you have the choice and the power consumption is not critical.

However, I would also learn C independently of the MCUs.

https://randomnerdtutorials.com/esp32-web-server-arduino-ide/
« Last Edit: March 16, 2024, 09:33:19 am by Aldo22 »
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2550
  • Country: us
Re: Getting started with microcontrollers
« Reply #39 on: March 16, 2024, 10:52:46 am »
A major advantage of ESPs is that they can be controlled via a web interface.
Basically, you don't need to buy any special displays, buttons, encoders or keypads, but you can control and view everything via a web browser.


What's the point of using a microcontroller if you don't have any of these?
Isn't the point of having a MCU to control or gather data from a piece of hardware?   :-//

If all you want to do is learn how to program in C/C++, I would go so far as to say don't use a MCU.
Just write Linux or Windows programs.  You don't have to deal with the extra hardware.
Personally, I get the most satisfaction and immediate gratification from doing graphics.
Learning basic C is fairly easy. 
The general language is not all that different from others: Basic, Pascal, Ada, etc.

You're not going to see the real benefits of C++ until you have something with
multiple copies of some complex object.  Years ago, I wrote a MIDI sequencer where I had four keyboards.
Each keyboard performed the exact same functions, with the four summed together in the end.
You just declare four of them like you would four integers.

   
 

Online Aldo22

  • Frequent Contributor
  • **
  • Posts: 695
  • Country: ch
Re: Getting started with microcontrollers
« Reply #40 on: March 16, 2024, 11:20:44 am »
A major advantage of ESPs is that they can be controlled via a web interface.
Basically, you don't need to buy any special displays, buttons, encoders or keypads, but you can control and view everything via a web browser.


What's the point of using a microcontroller if you don't have any of these?
Isn't the point of having a MCU to control or gather data from a piece of hardware?   :-//

Of course you need sensors, motors, perhaps other modules etc., but they can be used or controlled “headless”.
I only spoke of “displays, buttons, encoders or keypads”.
For example, you can place a weather station with an ESP32 on the balcony and read its values using your smartphone in the kitchen. 
There is no need for a button or a display on the MCU.
 

Online Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Getting started with microcontrollers
« Reply #41 on: March 16, 2024, 11:43:08 am »
An important point in the ESP32 and Arduino discussion is that the Arduino Uno R3 uses 5V voltages while the ESP32 uses 3.3V voltages.
The peripherals of each are different, the voltages very much determine what external elements you will be controlling.
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5029
  • Country: ro
  • .
Re: Getting started with microcontrollers
« Reply #42 on: March 16, 2024, 11:43:47 am »
You don't need a web interface  ... apparently it's too hard to get a usb to serial cable and send commands to your microcontroller through serial... or get info through the serial connection... no, let's add unnecessary power consumption for the wireless, let's add a whole tcp/ip stack to deal with it etc etc etc.

For one way communication a simple infrared led and an IO pin would be enough... like multimeters work, see for example Uni-T UT61E data logging feature, basically an infrared led and an infrared sensor in the connector that plugs in the meter.  The bonus of having isolation between your computer and the device can often be worth it.

edit : the keyword I was looking for was optocouplers... one way connection could be one optocoupler, bi-directional connection would be two optocouplers .. a simple 4 pin connector or trrs jack or rj11/rj45 to a usb - serial converter chip on a cable would be enough to connect to a computer with isolation guaranteed.
« Last Edit: March 16, 2024, 11:53:06 am by mariush »
 
The following users thanked this post: pcprogrammer

Online soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3167
  • Country: es
Re: Getting started with microcontrollers
« Reply #43 on: March 16, 2024, 12:09:57 pm »
OK, I have downloaded arduino-ide_2.3.2_Linux_64bit.AppImage , 190 MB and it runs. Now I need to receive the Arduino so i can plug it in..

I am beginning to gain confidence that I can do it ... even if I am doing it ten years later than I should have.

Thanks all for the encouragement.

All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 6203
  • Country: ro
Re: Getting started with microcontrollers
« Reply #44 on: March 16, 2024, 12:12:34 pm »
OK, so suppose I order the most popular and recommended choice, which seems to be the Arduino "classic" Uno Rev3 for $27.60 in Amazon (plus Danegeld)
https://www.amazon.com/Arduino-A000066-ARDUINO-UNO-R3/dp/B008GRTSV6/ref=sr_1_1_sspa

Better order this, at half the price you get 3 Arduino boards.

https://www.amazon.com/HiLetgo-ATmega328P-Controller-Development-Unsoldered/dp/B01DLIJQA2/ref=sr_1_11

I don't know this seller, the idea is to get a pack of a few boards.  Arduino 'nano' is the same as UNO R3, but better and cheaper.

The Arduino 'nano' includes all the features of the UNO R3 + some extra benefits that UNO R3 don't have.  'nano' boards are compatible with UNO R3, and they use the same microcontroller as UNO R3.

It is better to have more than one board, either as a spare, or to make two boards to talk to each other, one at each end of the communication wires, and such.  Even more, if you have 2 boards, you can use one board to debug the other one, I mean hardware debugging the other board, so to inspect/manually change the registers, its memory inside, the state of the bits peripheral ports, etc.

« Last Edit: March 16, 2024, 12:19:53 pm by RoGeorge »
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #45 on: March 16, 2024, 12:34:23 pm »
It is better to have more than one board, either as a spare, or to make two boards to talk to each other, one at each end of the communication wires, and such.  Even more, if you have 2 boards, you can use one board to debug the other one, I mean hardware debugging the other board, so to inspect/manually change the registers, its memory inside, the state of the bits peripheral ports, etc.

I was not aware that the ATmega328 used on the UNO and the nano support hardware debugging, but after looking at the datasheet it does have debugWIRE support. It needs to be enabled by programming a fuse bit and then uses the reset pin for the communication. When using breakpoints the FLASH is reprogrammed and can deteriorate faster than with normal use. Not the easiest to use since the IDE has to deal with this to.

The bigger ATmega or STM32 devices have better support for debugging via JTAG or SWD. Not sure if the Arduino IDE supports debugging.

For a beginner serial print is much easier for debugging. Just add it in the code and look at the serial monitor what is being send.

Offline zilp

  • Regular Contributor
  • *
  • Posts: 206
  • Country: de
Re: Getting started with microcontrollers
« Reply #46 on: March 16, 2024, 01:05:19 pm »
I am beginning to gain confidence that I can do it ... even if I am doing it ten years later than I should have.

Thanks all for the encouragement.

Given that you do know the electronics/logic side of things, and even have done assembly on an MCU-ish processor (by modern standards) ... I'll say that this will be easy. The area where there might be a lot to learn for you is probably more higher-level software abstractions, maybe modern communication systems (i.e., ethernet/IP), which is useful for complex systems, but you can do a lot even without that.

C in particular doesn't have that many concepts that would be completely foreign to an assembly programmer, it's mostly just a slightly more human-friendly way to write down the same thing, so chances are you'll primarily have to learn the syntax ... which shouldn't be that difficult if you are already familiar with the concepts represented by that syntax, and that especially so when that syntax heavily borrows from general math syntax that you are presumably also familar with. I mean, it's not really that hard to figure out what "a = b + c;" roughly does, right?
 

Online Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Getting started with microcontrollers
« Reply #47 on: March 16, 2024, 01:34:02 pm »
A simple and small library for Arduino that allows multitasking is SCoop.
I recommend it when you intend to run several independent processes at the same time.
It has not been updated for many years, but it works well.

https://github.com/fabriceo/SCoop

Edit: Example

Code: [Select]

/*
 Multiple Blinks

 Demonstrates the use of the SCoop library in the same way than with original Scheduler.h
 
 Hardware required :
 * LEDs connected to pins 11, 12, and 13

 created 8 Oct 2012
 by Cristian Maglie
 Modified by
 Scott Fitzgerald 19 Oct 2012
 
 This example code is in the public domain
 
 http://arduino.cc/en/Tutorial/MultipleBlinks
 
 Modified by Fabrice Oudert 8 Jan 2013
 https://code.google.com/p/arduino-scoop-cooperative-scheduler-arm-avr/
 
*/

// Include Scheduler since we want to manage multiple tasks.
#include <SCoop.h>  // instead of original #include <Scheduler.h>

#if defined(SCoopANDROIDMODE) && (SCoopANDROIDMODE == 1)
#else
#error "to run this example with SCoop you must set de parameter SCoopANDROIDMODE to 1 at the begining of "SCoop.h"
#endif

int led1 = LED_BUILTIN;
int led2 = 12;
int led3 = 11;


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

  // Setup the 3 pins as OUTPUT
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  // Add "loop2" and "loop3" to scheduling.
  // "loop" is always started by default.
  Scheduler.startLoop(loop2);
  Scheduler.startLoop(loop3);
 
  Scheduler.start(); // this is needed with SCoop, not with Android original Scheduler
}

// Task no.1: blink LED with 1 second delay.
void loop() {
uint32_t timer1=0;

  digitalWrite(led1, HIGH);

  // IMPORTANT:
  // When multiple tasks are running 'delay' passes control to
  // other tasks while waiting and guarantees they get executed.
  Scheduler.delay(1000);

  digitalWrite(led1, LOW);

  Scheduler.delay(1000);

}

// Task no.2: blink LED with 0.1 second delay.
void loop2() {
uint32_t timer1=0;
  digitalWrite(led2, HIGH);
  Scheduler.delay(100);

  digitalWrite(led2, LOW);
  Scheduler.delay(100);
}

// Task no.3: accept commands from Serial port
// '0' turns off LED
// '1' turns on LED
void loop3() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c=='0') {
      digitalWrite(led3, LOW);
      Serial.println("Led turned off!");
    }
    if (c=='1') {
      digitalWrite(led3, HIGH);
      Serial.println("Led turned on!");
    }
  }
}
« Last Edit: March 16, 2024, 01:40:45 pm by Picuino »
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 6203
  • Country: ro
Re: Getting started with microcontrollers
« Reply #48 on: March 16, 2024, 01:48:19 pm »
When using breakpoints the FLASH is reprogrammed and can deteriorate faster than with normal use. Not the easiest to use since the IDE has to deal with this to.
...
Not sure if the Arduino IDE supports debugging.

I don't use Arduino IDE too often, IIRC the old Arduino IDE (v1.x) don't have hardware debugging support, while the new IDE (2.x) has hardware debugging support, but for newer boards.  It is possible to use hardware debugging on nano and/or UNO boards, though with some extra hoops.  Hardware debugging is out of the scope of a getting started/beginner topic, but it is possible with either a 'nano' or a  'UNO' type of Arduino boards (or other Arduino models based on ATmega328, or on any other MCU that supports HW debugging).

Regarding the wearing out the memory while setting hardware breakpoints (talking for debugWIRE interface only), there are 2 methods.  One way is to insert a BREAK assembly instruction in the executable code, so the flash will be rewritten to insert execution breakpoints.  Another method is to use only a single breakpoint at a time, breakpoint that is sitting in a special register, dedicated to debugWIRE debugging, so without writing/wearing the flash.

Don't know by heart if ATmega328 has dedicated breakpoint register for debugWIRE, without digging the datasheets right now, I guess it does support hardware breakpoints without rewriting the flash.  Even if it doesn't, the wear out of the flash reprogramming is not so dramatic, it can last for month, if not years of hobby level reprogramming and debugging (ab)use.

Anyway, an 'Arduino nano' board is less than $2-$5/pcs, so not a big deal to treat them almost as consumables.  Develop on one board, and once the software debugging is finished, program another new board that will go to the final project.


LATER EDIT - checked the datasheet for ATmega328P (the MCU used in either the Arduino nano and/or Arduino UNO R3), and the debugging breakpoints in mega328P seems to be possible only by inserting a BREAK instruction in the flash memory code (so changing the debugging breakpoints will require a flash write for that page of flash only):  see page 222 of 294 in https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf
« Last Edit: March 16, 2024, 02:12:17 pm by RoGeorge »
 

Online soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3167
  • Country: es
Re: Getting started with microcontrollers
« Reply #49 on: March 16, 2024, 05:04:10 pm »
Better order this, at half the price you get 3 Arduino boards.

https://www.amazon.com/HiLetgo-ATmega328P-Controller-Development-Unsoldered/dp/B01DLIJQA2/ref=sr_1_11

I don't know this seller, the idea is to get a pack of a few boards.  Arduino 'nano' is the same as UNO R3, but better and cheaper.

The Arduino 'nano' includes all the features of the UNO R3 + some extra benefits that UNO R3 don't have.  'nano' boards are compatible with UNO R3, and they use the same microcontroller as UNO R3.

It is better to have more than one board, either as a spare, or to make two boards to talk to each other, one at each end of the communication wires, and such.  Even more, if you have 2 boards, you can use one board to debug the other one, I mean hardware debugging the other board, so to inspect/manually change the registers, its memory inside, the state of the bits peripheral ports, etc.
Thanks. That looks very interesting. I think I will get both the Arduino Uno to get started and keep these nano in reserve so that I can dedicate them to particular project once it's developed and I can continue to play with the Uno.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf