Demo 32: Simple Machine Learning - Artificial neural network demo using Arduino ESP32 - IoT Sharing

Hot

Tuesday, September 12, 2017

Demo 32: Simple Machine Learning - Artificial neural network demo using Arduino ESP32

1. Introduction
In this demo, I will make a simple Machine Learning - Artificial neural network system using Arduino ESP32. Certainly, I will not use ESP32 for training process; instead, i will use Python + numpy for training process. After training, I will use the result weights will be used by ESP32 for output calculation based on input.
Artificial neural network (ANN) systems are inspired by the biological neural networks. These systems can learn to do tasks by considering examples, without task-specific programming. One of most famous methods for ANN training is Backpropagation. You can refer here.
In this demo: ESP32 with 2 buttons (B1 and B2) with possible states: pressed (0) or released (1) and there is 1 LED output with possible state state on (1) or off (0). The LED output can be set based on the state of input.
B1 B2 LED
pressed pressed off
pressed released on
released pressed on
released released off
Figure: state of LED based on state of buttons
We will create an ANN to learn the table above. The network has structure:
Figure: ANN structure of demo
2. Hardware
Here we set input pins as INPUT_PULLUP so the schematic is simple and we can re-use the schematic in Demo 21.
 Figure: hardware connections
[ESP32 GIO12 - BUTTON 1 - GND]
[ESP32 GIO13 - BUTTON 2 - GND]
[ESP32 GIO14 - LED - GND]
3. Software
3.1 ANN is implemented by Python + numpy
I used the code here.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
 
epochs = 10000                                  # Number of iterations
inputLayerSize, hiddenLayerSize, outputLayerSize = 2, 3, 1
L = 0.1                                         # learning rate      
 
X = np.array([[0,0], [0,1], [1,0], [1,1]])      # Buttons states array
Y = np.array([ [0],   [1],   [1],   [0]])       # LED states array
 
def sigmoid (x): return 1/(1 + np.exp(-x))      # activation function
                                                # weights on layer inputs
Wh = np.random.uniform(size=(inputLayerSize, hiddenLayerSize))
Wz = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
 
for i in range(epochs):
 
    H = sigmoid(np.dot(X, Wh))                  # calculate forward part
    Z = np.dot(H,Wz)                            # 
    E = Y - Z                                   # calculate error
    dZ = E * L                                  # delta Z
    Wz +=  H.T.dot(dZ)                          # calculate backpropagation part
    dH = dZ.dot(Wz.T) * sigmoid_deriv(H)        # 
    Wh +=  X.T.dot(dH)                          # update hidden layer weights

print("**************** error ****************") 
print(E)
print("***************** output **************") 
print(Z)   
print("*************** weights ***************") 
print("input to hidden layer weights: ")     
print(Wh)
print("hidden to output layer weights: ")
print(Wz)

Figure: training process and weights output
3.2 Arduino ESP32
The Arduino code just do the forward part to calculate the output when getting the inputs.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <math.h>

#define B1  12
#define B2  13
#define LED 14

int X[1][2]    =   {{1,0}};
/*these matrices was calculated by python */
float W1[2][3] =   {{0.74000854,  4.47769531, -0.98692059}, 
                    {0.83034991,  4.48772758, -0.55733578}};
float W2[3][1] =   {{-6.17234487}, 
                    {4.8835918}, 
                    {1.28875386}};
float Wo1[1][3];
float sum = 0;
float Y = 0;

/*sigmoid function*/
float sigmoid (float x)
{
    return 1/(1 + exp(-x));
}


void setup()
{
  Serial.begin(115200);
  pinMode(B1, INPUT_PULLUP); 
  pinMode(B2, INPUT_PULLUP); 
  pinMode(LED, OUTPUT); 
  digitalWrite(LED, LOW);
}

void loop()
{
  X[0][0] = digitalRead(B1);
  X[0][1] = digitalRead(B2);
  printf("B1 = %d, B2 = %d\n", X[0][0], X[0][1]);
  
  /* calculate forward part based on weights */
  //hidden layer
  for(int i=0; i<1; i++)
  {
      for(int j=0;j <3; j++)
      {
          for(int k=0; k<2; k++)
          {
              sum += X[i][k]*W1[k][j];
          }
          Wo1[i][j] = sigmoid(sum);
          sum = 0;  
      }
  }
  //output layer
  for(int i=0; i<1; i++)
  {
      for(int j=0;j <1; j++)
      {
          for(int k=0; k<3; k++)
          {
              Y += Wo1[i][k]*W2[k][j];
          } 
      }
  }
  printf("Y = %f\n", (Y));
  Y = round(Y);
  digitalWrite(LED, int(Y));
  Y = 0;
  delay(1000);
}
4. Result

88 comments:

ppf said...

sigmoid derivative function is missing

def sigmoid_deriv(x):
f = sigmoid(x)
return f * (1 - f)

source : https://stackoverflow.com/questions/10626134/derivative-of-sigmoid

Great post.
Very helpfull to start experiments.
Lot of your other posts are also well done.
Thx.

Tech It Yourself said...

Thank friend,

this is feed forward part so you will not see derivative.
I am wrtting new blog about machine learning.
It is http://www.fossreview.com/
Hope you like it :)
Regards

sandeep saxena said...

I want this type of one.beacuse in recent days i searched this type of blog finally i got.thanks for this blog.
ccna Training in Chennai
ccna Training institute in Chennai
Python Training in Chennai
Python Classes in Chennai
Angularjs Training in Chennai
ccna Training in Velachery
ccna Training in Tambaram

Kayal said...

You provide the concept is very different and useful. Thank you for sharing with us and continuing the great blogging.
Unix Training in Chennai
Unix shell scripting Training in Chennai
Tableau Training in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Oracle Training in Chennai
Power BI Training in Chennai
Unix Training in Chennai
Unix shell scripting Training in Chennai

markson said...

This is significant for clients who need to break down information from alternate points of view and the investigation of one viewpoint prompts business addresses that should be analyzed from different viewpoints. data science course in pune

seoexpert said...

Nice Post...I have learn some new information.thanks for sharing.
ExcelR data analytics course in Pune | business analytics course | data scientist course in Pune

seoexpert said...

Such a very useful article. I have learn some new information.thanks for sharing.
data scientist course in mumbai

Manikanta said...

Such a very useful article. Very interesting to read this article. I have learn some new information.thanks for sharing. ExcelR

ravali said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
ExcelR data science

Komal said...

your article on data science is very good keep it up thank you for sharing.
Data Science course in Hyderabad

ExcelR Pune said...

Very nice blog here and thanks for post it.. Keep blogging...
ExcelR data science training

Priyanka said...

Attend The PMP Certification From ExcelR. Practical PMP Certification Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification.
ExcelR PMP Certification

ravali said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ExcelR data analytics courses

Jagna Co Kalani said...

Great Article
Data Mining Projects


Python Training in Chennai

Project Centers in Chennai

Python Training in Chennai

Excelrsolutions said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck... data science course

python training in vijayawada said...

This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

Data Science Course said...

Thanks for giving me the time to share such nice information. Thanks for sharing.

Data Science Course
Data Science Course in Marathahalli

Priyanka said...

Attend The Business Analytics Course From ExcelR. Practical Business Analytics Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course.
Business Analytics Course
Data Science Interview Questions

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data Science Course

DataScience Specialist said...

I have to search sites with relevant information ,This is a
wonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
machine learning course in hyderabad

CloudLearn ERP said...

This material makes for great reading. It's full of useful information that's interesting,well-presented and easy to understand. I like articles that are well done.
Best Data Science training in Mumbai

Data Science training in Mumbai


priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

Priyanka said...

Attend The Artificial Intelligence course From ExcelR. Practical Artificial Intelligence course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Artificial Intelligence course.
Artificial Intelligence Course

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

BestTrainingMumbai said...

I wish that I could take an idea, research it like you and put it on paper in the same fashion that I have just read. Your ideas are fantastic.
SAP training in Mumbai
Best SAP training in Mumbai
SAP training institute Mumbai

BestTrainingKolkata said...

I have to agree with the valid points you make in your article because I see things like you. Additionally, your content is interesting and really good reading material.
SAP training in Kolkata
Best SAP training in Kolkata
SAP training institute in Kolkata

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspried me to read more. keep it up.
Correlation vs Covariance

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression

Anirban Ghosh said...

Reading this article gave me many things to think about. You have some quality information here that any reader would enjoy. I share many of your views in this article.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
SAP training institute Kolkata

Dubai Dandb said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore

Thank you..

360digitmgas said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
data science course in coimbatore

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

IT said...

The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

priyanka said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

sudhan said...

Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating


Robotic Process Automation (RPA) Training in Chennai | Robotic Process Automation (RPA) Training in anna nagar | Robotic Process Automation (RPA) Training in omr | Robotic Process Automation (RPA) Training in porur | Robotic Process Automation (RPA) Training in tambaram | Robotic Process Automation (RPA) Training in velachery





EXCELR said...

Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.
Data Science Course in Hyderabad

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

Rohini said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.

Data Analyst Course

hrithiksai said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, best online data science courses

EXCELR said...

Thanks for sharing nice information data science training Hyderabad

Priyanka said...

Attend The Course in Data Analytics From ExcelR. Practical Course in Data Analytics Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Course in Data Analytics.
Course in Data Analytics

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

hrithiksai said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data sciecne course in hyderabad

hrithiksai said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data sciecne course in hyderabad

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm

EXCELR said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.Also Checkoutdata science course in Hyderabad

piyasd said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

Best said...

With the gradual increase of popularity in social media and other internet platforms, customers, clients is gradually becoming socially interlinked for almost 24*7. From a business point of view, it is an immense opportunity to target the probable customers that surely affect the business outcome and image. digital marketing course in hyderabad

Anirban Ghosh said...

The content that I normally go through in the recent times is nothing like what you have on paper. Thank you for writing this!
The-interface is the perfect solution for SAP training. Interface is one of best SAP training in Kolkata, India. We are providing very prompt and world-class SAP training, SAP training in Kolkata. It is also a renowned SAP training institute in Kolkata.
The Interface is a unique SAP institute in Kolkata, and a SAP training center in Kolkata. It also gives best guidance on SAP through online training and thus,
it is considered as the Best SAP training in Kolkata.You can here directly contact the institute for SAP training in Kolkata and for any information. So keep visiting our websites to get update on regular basis.

CloudLearn ERP said...

This splendid article really deserves a courteous bow down. I must own up that this article is very helpful.
Data Science training in Mumbai
Data Science course in Mumbai
SAP training in Mumbai

EXCELR said...

Attend The data science course in Hyderabad From ExcelR. Practical data science course in Hyderabad Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The data science course in Hyderabad. data science course in Hyderabad

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

hrithiksai said...

Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data science course

ek said...

Your work is very good, and I appreciate you and hopping for some more informative posts
<a href="https://www.excelr.com/business-analytics-training-in-pune/”> Courses in Business Analytics </a>
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job !

Rohini said...


Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle.
data science course

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained

Priyanka said...

Attend The Data Science Training Bangalore From ExcelR. Practical Data Science Training Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Training Bangalore.
Data Science Training Bangalore

Unknown said...

Very interesting to read this article.I would like to thank you for the efforts. I also offer Data Scientist Courses data scientist courses

EXCELR said...

Very interesting to read this article.I would like to thank you for the efforts. I also offer Data Scientist Courses data scientist courses

Priyanka said...

Attend The Business Analytics Course From ExcelR. Practical Business Analytics Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course.
Business Analytics Course

priyanka said...

very well explained .I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

hrithiksai67 said...

Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

prathyusha said...

Truly overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. Much obliged for sharing.business analytics course in Hyderabad

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Logistic Regression explained
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm

Suryaprakash said...

I liked this blog.. I got some clear information from this blog.. Thanks for taking a time to share this blog...
graphic design courses in tambaram
graphic design courses in Porur
Artificial Intelligence Course in Tambaram
Artificial Intelligence Course in Velachery
Artificial Intelligence Course in porur

maurya said...

I was really impressed to see this blog, it was very interesting and it is very useful for all.
how to convert list to string in python
data structures using python
polymorphism in oops
numpy in python
python interview questions and answers for testers
convert string to list python

Morgan said...

When we think of Data, we actually tend to think of 0s and 1s and most the Information Technology industries like social media, e-commerce, the block chain, crypto-technology etc. data science course syllabus

arshiya said...

Nice article, I got new information from your article, keep sharing.
common seo mistakes
artificial intelligence future scope
angularjs charts
benefits of automation testing
benefits of automation
angularjs interview questions and answers pdf

saketh321 said...

very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post! ExcelR Business Analytics Courses

Devi said...

This is an excellent blog. Really very creative and informative content.
artificial intelligence advantages
application of asp net
definition of hadoop
devops tools java
selenium interview questions and answers pdf
selenium webdriver interview questions and answers

Deekshitha said...

Very informative blog
Join 360digiTMG for the best courses
data science training in Pune

Anna Schafer said...

I high appreciate this post. It’s hard to find the good from the bad sometimes, but I think you’ve nailed it! would you mind updating your blog with more information? machine learning interview questions

Unknown said...

hello sir,
thanks for giving that type of information. I am really happy to visit your blog.Leading Solar company in Andhra Pradesh

Flora said...

Glad to see your article, thanks for taking the time to share the blog.
how to improve testing skills
about banking process
languages for data science
what is php used for in web development
digital marketing interview questions and answers for freshers
hadoop interview questions and answers pdf

Data science course training institutes in Marthahalli said...

Thanks for sharing a useful info.

Srigokul said...


Nice Blog. Thanks for Sharing this useful information...

Data science training in chennai
Data science course in chennai

data scientist said...

Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

Data Science Course in Raipur

Mallela said...

Thanks for posting the best information and the blog is very helpful.Data science course in Faridabad

vé máy bay từ singapore về việt nam said...

Aivivu chuyên vé máy bay, tham khảo

vé máy bay vietjet từ hàn quốc về việt nam

vé máy bay hà nội sài gòn tháng 4

mua vé máy bay đi hà nội giá rẻ

giá vé máy bay đi cam ranh

gia ve may bay di my

Data Analytics Courses in Bangalore said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
Data Analytics Courses in Bangalore

Deekshitha said...

Informative blog
Data Science Course in Pune

Deekshitha said...

Informative blog
data scientist course in Bangalore

Deekshitha said...

Informative blog
Data Science Course in India

Data Science Institute in Bangalore - 360DigiTMG said...

Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

data science institute in bangalore

Unknown said...

This material makes for great reading. It's full of useful information that's interesting,well-presented, and easy to understand. I like articles that are well done.
https://socialprachar.com/
Artificial Intelligence Training and Course in Hyderabad

Maneesha said...

Your amazing insightful information entails much to me and especially to my peers. Thanks a ton; from all of us.
data scientist training and placement

Thường mất vài phút để quảng cáo xuất hiện trên trang nhưng thỉnh thoảng, việc này có thể mất đến 1 giờ. Hãy xem hướng dẫn triển khai mã của chúng tôi để biết thêm chi tiết. Ðã xong